Node.js 12的TypeScript tsconfig设置? [英] TypeScript tsconfig settings for Node.js 12?
问题描述
用于输出要在Node.js 12上运行的代码的最佳TypeScript tsconfig
设置是什么?
What is the optimal TypeScript tsconfig
settings for outputting code that's going to be run on Node.js 12?
推荐答案
从Node.js 12.0.0
开始,支持100%的ES2019.如果您知道要定位该版本或更高版本,则最佳配置应如下所示:
As of Node.js 12.0.0
, 100% of ES2019 is supported. If you know that you are targeting that version or newer, the optimal config would look like this:
-
"module": "commonjs"
Node.js可以添加ES模块,但是现在我们必须坚持使用CommonJS.
Node.js is on it's way to add ES-Modules, but for now we'll have to stick with CommonJS.
"target": "es2019"
这告诉TypeScript可以输出具有ES2019中的功能的JavaScript 语法.实际上,这意味着它将例如输出对象的剩余/扩展属性& async/await语法,而不是嵌入polyfill.
This tells TypeScript that it's okay to output JavaScript syntax with features from ES2019. In practice, this means that it will e.g. output object rest/spread properties & async/await syntax instead of embedding a polyfill.
"lib": ["es2019", "es2020.bigint", "es2020.string", "es2020.symbol.wellknown"]
这告诉TypeScript可以使用ES2019或更早版本中引入的功能和属性.实际上,这意味着您可以使用String.prototype.trimStart
和Array.prototype.flat
.
This tells TypeScript that it's okay to use functions and properties introduced in ES2019 or earlier. In practice, this means that you can use e.g. String.prototype.trimStart
and Array.prototype.flat
.
除了ES2019,Node.js 12还支持BigInt
& matchAll
来自ES2020,因此我们包括了ES2020的其他定义.
In addition to ES2019, Node.js 12 also supports BigInt
& matchAll
from ES2020, therefor we include the additional definitions from ES2020.
因此,完整的配置为:
{
"compilerOptions": {
"lib": ["es2019", "es2020.bigint", "es2020.string", "es2020.symbol.wellknown"],
"module": "commonjs",
"target": "es2019"
}
}
如果您要定位Node.js 12.9.0
或更高版本,则只需指定"lib": ["es2020"]
,因为该版本支持ES2020中引入的所有新功能和属性.不过,它不支持新的JavaScript 语法,因此您仍然必须使用"target": "es2019"
.
If you are targeting Node.js 12.9.0
or newer, you can simply specify "lib": ["es2020"]
as that version supports all new functions and properties introduced in ES2020. It doesn't support the new JavaScript syntax though, so you still have to stay on "target": "es2019"
.
因此,完整的配置为:
{
"compilerOptions": {
"lib": ["es2020"],
"module": "commonjs",
"target": "es2019"
}
}
如果您正在运行Node.js 14,则可以在此处看到我的针对Node.js 14的类似答案
如果您正在运行Node.js 10,则可以在此处看到我的针对Node.js 10的类似答案
If you are running Node.js 10 you can see my similar answer for Node.js 10 here
如果您正在运行Node.js 8,则可以在此处看到我的针对Node.js 8的类似答案
If you are running Node.js 8 you can see my similar answer for Node.js 8 here
这篇关于Node.js 12的TypeScript tsconfig设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!