什么 TypeScript 配置产生最接近 Node.js 16 功能的输出? [英] What TypeScript configuration produces output closest to Node.js 16 capabilities?

查看:67
本文介绍了什么 TypeScript 配置产生最接近 Node.js 16 功能的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近发布的 Node.js 16 带来了对 ES2021 的更新支持和对 ES 模块的稳定支持,如何配置 TypeScript 以输出利用所有这些新功能的 JavaScript 代码?

Node.js 16 was recently released bringing updated support for ES2021 and stable support for ES modules, how can TypeScript be configured to output JavaScript code that takes advantage of all these new features?

推荐答案

从 Node.js 16.0.0 开始,100% 支持 ES2021,并且对 ES Modules 的支持稳定!如果您知道自己的目标是该版本或更新版本,则最佳配置如下所示:

As of Node.js 16.0.0, 100% of ES2021 is supported, and support for ES Modules is stable! If you know that you are targeting that version or newer, the optimal config would look like this:

  • 模块":ES2020" &moduleResolution":节点"

Node.js 16 支持加载模块而不是旧的 CommonJS 格式,我们必须告诉 TypeScript 我们正在使用 Node.js 的规则来解析模块.

Node.js 16 support loading modules instead of the old CommonJS format, we do have to tell TypeScript that we are using Node.js's rules for resolving modules.

allowSyntheticDefaultImports":真

为了提供向后兼容性,Node.js 允许您使用默认导入来导入 CommonJS 包.这个标志告诉 TypeScript 可以在 CommonJS 模块上使用 import.

To provide backwards compatibility, Node.js allows you to import CommonJS packages with a default import. This flag tells TypeScript that it's okay to use import on CommonJS modules.

目标":ES2021"

这告诉 TypeScript 可以输出带有 ES2021 特性的 JavaScript 语法.在实践中,这意味着它将例如输出逻辑赋值运算符 &async/await 语法,而不是嵌入 polyfill.

This tells TypeScript that it's okay to output JavaScript syntax with features from ES2021. In practice, this means that it will e.g. output logical assignment operators & async/await syntax instead of embedding a polyfill.

lib":[ES2021"]

这告诉 TypeScript 可以使用在 ES2021 或更早版本中引入的函数和属性.在实践中,这意味着您可以使用例如Promise.anyString.prototype.replaceAll.

This tells TypeScript that it's okay to use functions and properties introduced in ES2021 or earlier. In practice, this means that you can use e.g. Promise.any and String.prototype.replaceAll.

因此完整的配置是:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "lib": ["ES2021"],
    "module": "ES2020",
    "moduleResolution": "node",
    "target": "ES2021"
  }
}

除此之外,我们还需要告诉 Node.js 将本项目中的 .js 文件视为 ES Modules.这样做的原因是 Node.js 必须保持与为旧 Node.js 版本编写的代码的向后兼容性.这可以通过将 "type": "module" 添加到您的 package.json:

In addition to this, we also need to tell Node.js to treat .js files in this project as ES Modules. The reason for this is that Node.js had to maintain backwards compatibility with code written for older Node.js versions. This can be done by adding "type": "module" to your package.json:

{
  "type": "module"
}

如果您来自早期版本的 Node.js,另一个变化是 导入文件时的文件扩展名现在是强制性的.这意味着您必须在本地导入的末尾写出 .js.请注意,这是 .js,即使您要导入的 TypeScript 文件实际上具有文件扩展名 .ts.这似乎有点令人困惑,但 一位 TS 贡献者的评论解释了为什么会这样.

Another change if you are comming from an earlier version of Node.js is that the file extension when importing files are now mandatory. This means that you must write out .js at the end of your local imports. Note that this is .js even though you are importing a TypeScript file that actually has the file extension .ts. This might seem a bit confisuing but this comment from one of the TS contributors explains why that is.

以下是如何编写 import 语句的一些示例:

Here are some examples of how to write your import statements:

// Built-in Node.js modules
import { readFileSync } from 'fs'

// CommonJS packages from Npm
import md5File from 'md5-file'

// The local file "a.ts"
import { a } from './a.js'


如果你现在想坚持使用 CommonJS,为了避免上面解释的警告,你可以使用以下配置:


If you want to stick with CommonJS for now, to avoid the caveats explained above, you can use the following config:

{
  "compilerOptions": {
    "lib": ["ES2021"],
    "module": "CommonJS",
    "target": "ES2021"
  }
}


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

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

If you are running Node.js 12 you can see my similar answer for Node.js 12 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

这篇关于什么 TypeScript 配置产生最接近 Node.js 16 功能的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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