nodejs 中的 TypeScript 模块导入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12958479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:29:59  来源:igfitidea点击:

TypeScript module import in nodejs

node.jstypescript

提问by user1756840

What is best practice for importing modules in nodejs with typescript? I come from c# background so I want to do something like this

使用打字稿在 nodejs 中导入模块的最佳实践是什么?我来自 c# 背景所以我想做这样的事情

MyClass.ts

MyClass.ts

module MyNamespace {
    export class MyClass {
    }
}

app.ts

应用程序

// something like using MyNamespace
new MyNamespace.MyClass();

or

或者

MyClass.ts

MyClass.ts

export class MyClass {
}

app.ts

应用程序

import MyClass = module("MyClass")
new MyClass();

I know I can do this and it will work, but then I have to think up for two names for each class

我知道我可以做到这一点并且它会起作用,但是我必须为每个班级想出两个名字

import MyClass2 = module("MyClass")
new MyClass2.MyClass();

Point is separating classes to multiple .ts files (preferably one file per class). So question is, how is this done?

重点是将类分离到多个 .ts 文件(最好每个类一个文件)。那么问题来了,这是怎么做到的?

回答by Valentin

You have two choices here:

您在这里有两个选择:

If you insist on using CommonJS or AMD modules, then you will have to use externalmodules just the way you described it in your question. Whether or not you use a module to declare your own namespace is mostly a matter of taste. The only way to circumvent the issue of specifying two names is to create a variable that aliases the type:

如果您坚持使用 CommonJS 或 AMD 模块,那么您将不得不按照您在问题中描述的方式使用外部模块。您是否使用模块来声明您自己的命名空间主要是一个品味问题。规避指定两个名称问题的唯一方法是创建一个为该类型取别名的变量:

mymodule.ts

mymodule.ts

export module MyNamespace {
    export class MyClass {
    }
}

app.ts

应用程序

import ns = require('mymodule');
var myclass = new ns.MyNamespace.MyClass();
var myclassalias = ns.MyNamespace.MyClass;
var myclass2 = new myclassalias();

Your other option is to use internalmodules which are mostly used to structure your code internally. Internal modules are brought into scope at compile time using reference paths.

您的另一种选择是使用内部基本都是用于内部构造代码模块。内部模块在编译时使用引用路径引入范围。

mymodule.ts

mymodule.ts

module MyNamespace {
    export class MyClass {
    }
}

app.ts

应用程序

///<reference path='mymodule.ts'/>
var myclass = new MyNamespace.MyClass();

I think you'll have to decide for yourself which of those two approaches is more appropriate.

我认为您必须自己决定这两种方法中的哪一种更合适。

回答by Anderson Green

You can import TypeScript modules into a node.js file using the typescript-requiremodule, which was created for this specific purpose.

您可以使用typescript-require模块将 TypeScript 模块导入到 node.js 文件中,该模块是为此特定目的而创建的。

回答by Burt_Harris

I would recommend against using the explicit module(or namespace) keyword, it's a vestigial remnant of an earlier time.* You generally don't need them because any typescript file with a top-level importor exportis automatically a module. Your second myModule.tsexample was good.

我建议不要使用显式module(或namespace) 关键字,它是早期的残余。* 您通常不需要它们,因为任何具有顶级importexport自动模块的打字稿文件。您的第二个myModule.ts示例很好。

export class MyClass {
   ...
}

But when you import it to another typescript module, you'll want to use something like this:

但是当你将它导入另一个打字稿模块时,你会想要使用这样的东西:

import { MyClass } from './myModule';
var myInstance = new MyClass();

Personally, I don't like repetitiveness of line 1, but it is what the language calls for, so I've learned to accept it. I think the utility of this syntax isn't apparent unless you abandon the file-per-class pattern. You pick and choose what names to import from the module, so that no unintended namespace pollution occurs.

就我个人而言,我不喜欢第 1 行的重复性,但这是语言所要求的,所以我已经学会了接受它。我认为这种语法的效用并不明显,除非您放弃文件每类模式。您挑选并选择要从模块导入的名称,这样就不会发生意外的命名空间污染。

An alternative import syntax pulls in all names from the module, but you must qualify the names with the module when you use them. Therefore it is also name collision resistant.

另一种导入语法从模块中提取所有名称,但在使用它们时必须用模块限定名称。因此,它也具有抗碰撞性。

import * as myModule from './myModule';
var myInstance = new myModule.MyClass();

There are exceptions to the general rule about not needing module/ namespacekeywords, but don't start by focusing on them. Think file == module.

关于不需要module/namespace关键字的一般规则也有例外,但不要一开始就关注它们。想想文件 == 模块