Node.js 12 的 TypeScript tsconfig 设置? [英] TypeScript tsconfig settings for Node.js 12?

查看:23
本文介绍了Node.js 12 的 TypeScript tsconfig 设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输出将在 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:

  • 模块":commonjs"

Node.js 正在添加 ES-Modules,但现在我们必须坚持使用 CommonJS.

Node.js is on it's way to add ES-Modules, but for now we'll have to stick with CommonJS.

目标":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.trimStartArray.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 16,您可以在此处查看我的 Node.js 16 的类似答案

如果您正在运行 Node.js 14,您可以在此处查看我的 Node.js 14 的类似答案

If you are running Node.js 14 you can see my similar answer for Node.js 14 here

如果您正在运行 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆