typescript 理解 tsconfig 中的“目标”和“模块”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41993811/
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
Understanding "target" and "module" in tsconfig
提问by refactor
Below is my tsconfig.json file where I have set target to "es5" and module to "es6"
下面是我的 tsconfig.json 文件,我将目标设置为“es5”并将模块设置为“es6”
{
"compilerOptions": {
"target": "es5",
"module": "es6"
}
}
My question is because modules [import / export ] are part of es6 and NOT es5 , the transpiled javascript code should not be having import / export statements. But the javascript code that is generated is having import / export statements even though the target is es5 , how is it possible ?
我的问题是因为模块 [import/export] 是 es6 而不是 es5 的一部分,转译的 javascript 代码不应该有导入/导出语句。但是即使目标是 es5,生成的 javascript 代码也有导入/导出语句,这怎么可能?
采纳答案by jims
The module system is independent of the language implementation. ES6 (ES2015) modules use the import/export syntax, and it is up to the module loader to interpret that.
模块系统独立于语言实现。ES6 (ES2015) 模块使用导入/导出语法,由模块加载器来解释。
Here you have specified using the ES2015 module system, so that enables the ES6 module syntax.
此处您已指定使用 ES2015 模块系统,以便启用 ES6 模块语法。
The Javascript itself may target ES5, and use only ES5 features, but it is theoretically possible to use a module loader with that code that operates with ES2015 module syntax. Although it is possible, it is not necessarily something you would want to do. It is more common to use CommonJS or AMD modules with ES5 Javascript.
Javascript 本身可能针对 ES5,并且仅使用 ES5 功能,但理论上可以使用模块加载器以及使用 ES2015 模块语法操作的代码。尽管这是可能的,但它不一定是您想要做的事情。在 ES5 Javascript 中使用 CommonJS 或 AMD 模块更为常见。
Apparently this combination was not even allowed before Typescript 2.0. In the Typescript 2.0 release notes, it says:
显然,在 Typescript 2.0 之前甚至不允许这种组合。在 Typescript 2.0 发行说明中,它说:
"Previously flagged as an invalid flag combination, target: es5 and ‘module: es6' is now supported. This should facilitate using ES2015-based tree shakers like rollup."
“以前被标记为无效的标志组合,现在支持 target: es5 和 'module: es6'。这应该有助于使用基于 ES2015 的摇树器,如 rollup。”
回答by Alexey Grinko
To supplement the previous answer, in 2020 there are 4 TS config options that define the module resolution and compilation output:
为了补充之前的答案,2020 年有 4 个 TS 配置选项用于定义模块分辨率和编译输出:
- module
- target
- lib
- moduleResolution
- 模块
- 目标
- 库
- 模块分辨率
The first 3 affect your output, while the latter affects the way the compiler searches for your modules to resolve them and bundle.
前 3 个影响您的输出,而后者影响编译器搜索您的模块以解析它们并捆绑的方式。
Here's a great and concise article about these options: https://medium.com/@tommedema/typescript-confusion-tsconfig-json-module-moduleresolution-target-lib-explained-65db2c44b491
这是一篇关于这些选项的精彩而简洁的文章:https: //medium.com/@tommedema/typescript-confusion-tsconfig-json-module-moduleresolution-target-lib-explained-65db2c44b491
Additionally, a doc about module resolution: https://www.typescriptlang.org/docs/handbook/module-resolution.html
此外,关于模块解析的文档:https: //www.typescriptlang.org/docs/handbook/module-resolution.html