【问题标题】:TypeScript and Node and importingTypeScript 和 Node 以及导入
【发布时间】:2015-03-10 04:30:35
【问题描述】:

我正在尝试在 Node.js 中使用 TypeScripttypescript-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 似乎都没有正确包含文件,所以我不能重用它。所以我忘记了什么?

【问题讨论】:

    标签: node.js import typescript


    【解决方案1】:

    在发布之前,我找到了解决方案。我正在尝试在文件Dictionary.ts 上导出接口,喜欢它:

    export interface ... { ... }
    

    但我需要export =。喜欢:

    interface Name { ... }
    export = Name;
    

    【讨论】:

      猜你喜欢
      • 2019-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-12
      • 2018-05-14
      • 2017-08-02
      • 2016-03-28
      • 2018-11-17
      相关资源
      最近更新 更多