TypeScript和Node以及导入

时间:2015-03-10 04:30:35

标签: node.js import typescript

我正在尝试将 Node.js 中的 TypeScript typescript-require一起使用。所以我就这样说:

// index.js, line 1.
require("typescript-require")({ "nodeLib": true });

所以我加载了Main.ts文件。喜欢它:

// index.js, line 2.
require("Main.ts").init();

所以我有一个Dictionary界面:

// Main.ts, line 8.
interface Dictionary<TValue> {
    [index: string]: TValue;
}

当我将此代码直接放在Main.ts上时,它运行正常,就像它:

// Main.ts, line 12.
var list: Dictionary<number> = {};

但我想将DictionaryMain分开。允许其他文件使用此接口而不重复它。 在这里开始我的问题。我不知道如何做到这一点。我尝试了很多东西,比如使用referenceimport require,我收到了所有方法的错误。

/// Without any import, I get an obvious error; or,
/// <reference path="Dictionary" />; or,
/// <reference path="Dictionary.ts" />; or,
/// <reference path="./Dictionary.ts" />; or,
import Dictionary = require("Dictionary");
>> Main.ts(6,18): error TS2304: Cannot find name 'Dictionary'.

import Dictionary = require("Dictionary.ts");
>> Main.ts(1,29): error TS2304: Cannot find name 'Dictionary'.
>> Main.ts(6,18): error TS2315: Type 'any' is not generic.

在所有情况下,似乎Main.ts未正确包含文件,因此我无法重复使用该文件。所以我忘了什么?

1 个答案:

答案 0 :(得分:0)

在我发布之前,我找到了解决方案。我在文件Dictionary.ts上尝试导出界面:

export interface ... { ... }

但我需要export =它。像:

interface Name { ... }
export = Name;