从Node开始.js 12.0.0
,12.0.0
%支持ES2019.如果您知道您的目标是该版本或更新版本,那么最佳配置如下所示:
"module": "commonjs"
node .js正在添加ES模块,但现在我们必须坚持使用CommonJS.
"target": "es2019"
这告诉TypeScript,可以输出带有ES2019特性的JavaScript syntax.实际上,这意味着它将输出对象的静止/扩展属性等;异步/等待语法,而不是嵌入polyfill.
"lib": ["es2019", "es2020.bigint", "es2020.string", "es2020.symbol.wellknown"]
这告诉TypeScript可以使用ES2019或更早版本中引入的functions and properties.实际上,这意味着您可以使用例如String.prototype.trimStart
和Array.prototype.flat
.
除ES2019外, node .js 12还支持BigInt
&;matchAll
,因此我们纳入了ES2020的其他定义.
因此,完整配置将是:
{
"compilerOptions": {
"lib": ["es2019", "es2020.bigint", "es2020.string", "es2020.symbol.wellknown"],
"module": "commonjs",
"target": "es2019"
}
}
If you are targeting Node.js 100 or newer,您可以简单地指定"lib": ["es2020"]
,因为该版本支持ES2020中引入的所有新functions and properties.不过,它不支持新的JavaScript syntax,所以您仍然必须使用"target": "es2019"
.
因此,完整配置将是:
{
"compilerOptions": {
"lib": ["es2020"],
"module": "commonjs",
"target": "es2019"
}
}
如果您正在运行Node.js 16你可以看到我的similar answer for Node.js 16 here
如果您正在运行Node.js 14你可以看到我的similar answer for Node.js 14 here
如果您正在运行Node.js 10你可以看到我的similar answer for Node.js 10 here
如果您正在运行Node.js 8你可以看到我的similar answer for Node.js 8 here