NodeJS / Typescript导出/导入的正确解释? [英] Proper explanation for NodeJS/Typescript Export / Import?
问题描述
有人可以使用Typescript准确解释导出和导入在NodeJS中的工作原理吗?
我的设置是:
- NodeJS
- 打字稿中的所有内容
- TSLint
- 打字
我正在搞乱出口/进口,而不是做一些正确的编码,它让我疯狂,并且找不到任何正确的解释它是如何工作的。
导入
您能否解释以下内容:
var module = require(module);
import module = require(module);来自module的
导入模块;
从模块导入{something};
import *作为模块来自module;
导出
<你能解释一下吗?
export = something;
导出默认值;
导出界面| class something;
问题
- 如何在不启动类的情况下正确导出/导入类
- 如何在启动/导入类的情况下正确导出/导入类(构造确实运行)
- 如何正确导出/导入类和接口
- 如何正确导出/导入类和多个接口
- 什么时候使用模块声明,如果我尝试使用它们,为什么tslint会向我抛出错误。
我似乎无法找到正确的导出与导入的方法,因此我的IDE不会被红色覆盖并向我抛出数百个错误。
一般问题
- 有没有办法不使用nodejs / typescript / tslint获取错误?
- TSLint正在为我几乎所有打字的东西尖叫......这是非常令人沮丧的,因为大多数错误都有0个解释ORS。是否值得使用它?
问题列表一直在继续,但我确定一旦上面的一些问题得到解答,我可以接受其余的。
谢谢你,对不起这样的一般性问题,但我的挫折程度刚刚达到顶峰......
同意,导入/导出语法至少有两个原因令人困惑:
- commonjs语法:
var module = require(module);
有效,但这是commonjs - > no typings - 它发生了变化:语法
import x = require('y'
)现已弃用于TS
TL; DR; :使用TS 1.5中引入的'es6 style'语法
'最佳'资源在TS中导入/导出我知道是这个
总的来说,我建议阅读此内容优秀的手册,它将为您的大多数问题提供答案
要导入
从默认导出
某物
被导出为默认(单个)导出,即导出默认值
in ts / es6
使用
import模块中的东西
您实际上可以导入具有不同名称的默认导出。 从'module'
导入SomethingElse也可以使用
来自命名导出
使用
export {Something} $将某些
导出为module中的命名导出c $ c>或导出类|接口在ts / es6中的东西{}
你想只导入那个,使用
从模块导入{Something}
您想导入从名称空间 mod
下的module导出的所有内容
import * as mod frommodule
然后使用 const c:mod.Something = whatever
要导出
参见上面的 import
表单 export = something不推荐使用
来支持新的ES6样式语法。它主要在定义文件中找到,表示js库导出单个函数/对象e。 g。 module.exports =某事
。
你应该尝试使用什么
使用ES6样式语法并避免默认
导出:它们的优点是可以使用不同的名称导入它们,但
- 无法重新导出默认导入,如果您正在编写库,这可能会有问题
- 他们会混淆很多重构时的IDE(更不用说你自己)
- 命名导出实际上可以在导入时进行本地重命名,即
从模块导入{Something as SomethingElse}
具体而言,出口任何需要出口的东西并专门导入
在 api.ts
导出界面MyInterface {
}
导出类MyClass {
}
在 main.ts
从'导入{MyInterface,MyClass}。 / api'
Linting
有很多优秀的IDE提供优秀的linting:VSCode,Atom Typescript和Webstorm命名一个受欢迎的少数人,前两个是免费的,第三个甚至为你管理进口。
Could someone please explain exactly how exports and imports work in NodeJS using Typescript?
My setup is:
- NodeJS
- Everything in Typescript
- TSLint
- Typings
I am messing about with exports/imports instead of doing some proper coding, its driving me nuts, and cannot find any proper explanation of how it works.
Import
Can you please explain following:
var module = require ("module");
import module = require("module");
import module from "module";
import {something} from "module";
import * as module from "module";
Export
Can you please explain following
export = something;
export default something;
export interface|class something;
Questions
- How do properly export/import a class without initiating it
- How do properly export/import a class with it being initiated (construct did run)
- How do I properly export/import a class and interface
- How do I properly export/import class and multiple interfaces
- When to use modules declarations, what are they good for and why is tslint throwing errors at me if I try to use them.
I cannot seem to find proper way of doing exports vs. imports so my IDE is not covered in red and throwing hundreds of errors at me.
General Questions
- Is there even a way to not get errors using nodejs/typescript/tslint?
- TSLint is screaming at me for almost anything I type... it is extremely frustrating since there is 0 explanation to most of the errors. Is it even worth using it?
List of questions goes on and on, but im sure once some of the above is answered, I can pick up on the rest.
Thank you and sorry for such a general question, but my frustration level just reached the top...
Agreed, import/export syntax is confusing for at least two reasons:
- the commonjs syntax:
var module = require ("module");
works but that is commonjs -> no typings - it changed: the syntax
import x = require('y'
) is now deprecated in TS
TL;DR;: Use the 'es6 style' syntax introduced in TS 1.5
The 'best' resource on import/export in TS I know is this
Overall I recommend reading this excellent handbook which will provide answers to most of your questions
To import
From a default export
Something
was exported as a default (single) export ie export default Something
in ts/es6
Use
import Something from "module"
You can actually import a default export with a different name. import SomethingElse from 'module'
will also work
From named exports
Something
was exported as a named export in "module" using export {Something}
or export class|interface Something{}
in ts/es6
You want to import only that, use
import {Something} from "module"
You want to import everything that is exported from "module" under the namespace mod
import * as mod from "module
Then use const c:mod.Something = whatever
To export
See import
s above
The form export = something
is deprecated in favor of the new ES6 style syntax. It is mostly found in definition files to express the fact that a js library exports a single function/object e.g. module.exports=something
.
What you should try using
Use ES6 style syntax and avoid default
exports: they have the advantage that they can be imported using a different name but
- a default import cannot be re-exported, which may be problematic if you are writing a library
- they will confuse a lot of IDEs (not to mention yourself) when refactoring
- a named export can actually be locally renamed when imported i.e.
import {Something as SomethingElse} from "module"
Concretely, export whatever needs to be exported and import it specifically
In api.ts
export interface MyInterface {
}
export class MyClass {
}
In main.ts
import {MyInterface, MyClass} from './api'
Linting
There are a lot of good IDEs out there that provide excellent linting: VSCode, Atom Typescript and Webstorm to name a popular few, the first two being free and the third one even manages the imports for you.
这篇关于NodeJS / Typescript导出/导入的正确解释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!