简体   繁体   English

TypeScript和Node并导入

[英]TypeScript and Node and importing

I'm trying to use TypeScript inside Node.js with typescript-require . 我试图用打字稿的Node.jstypescript-require So I init it like: 所以我像这样初始化它:

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

And so I load the Main.ts file. 因此,我加载了Main.ts文件。 Like it: 喜欢它:

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

So I have a Dictionary interface: 所以我有一个Dictionary界面:

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

When I put this code directly on Main.ts it run fine, like it: 当我将这段代码直接放在Main.ts它运行良好,就像这样:

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

But I like to separate the Dictionary from Main . 但是我喜欢将DictionaryMain分开。 To allow others files use this interface without duplicate it. 要允许其他文件使用此接口而不重复它。 And here starts my problem. 这开始了我的问题。 I don't know how I can do that. 我不知道该怎么做。 I tried a lot things, like use reference and import require , and I receive errors from all methods. 我尝试了很多事情,例如使用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.

In all cases, seems that Main.ts doesn't include the file correctly, so I can't reuse that. 在所有情况下, Main.ts似乎都没有正确包含该文件,因此我无法重复使用该文件。 So I'm forgetting something? 所以我忘记了什么?

Before I post it I found the solution. 在发布之前,我找到了解决方案。 I was trying export interface on file Dictionary.ts like it: 我正在尝试像这样的Dictionary.ts文件上的导出接口:

export interface ... { ... }

But I need export = it. 但是我需要export =它。 Like: 喜欢:

interface Name { ... }
export = Name;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM