输出将在 node 上运行的代码的最佳TypeScript tsconfig设置是什么.js 12?

推荐答案

从Node开始.js 12.0.012.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.trimStartArray.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

Node.js相关问答推荐

使用NodeJS在S3上传文件时的格式问题

运行JEST测试时找不到模块&q;错误

ForbidenError:使用Express.js的CSRF令牌无效

Postgressql的BIGSERIAL自增序列,即使由于唯一约束错误没有创建行,也会自动增加

对 google api v3 的 Axios 请求返回加密(?)数据

无服务器部署使用无服务器组合抛出`spawn serverless ENOENT`

将环境变量从 GitHub 操作传递到 json

如何使用 Jest 模拟异步函数的延迟时间

使用Typescript 时我应该避免循环导入吗?

如果 express.js (node.js) http 请求在完成之前关闭会发生什么?

bash:npm:找不到命令?

如何在客户端使用 node.js 模块系统

如何解决 Socket.io 404(未找到)错误?

在 Node.js 中使用公钥加密数据

NodeJS 中的 HTTPS 请求

卸载代码/模块

NodeJS:如何调试检测到 EventEmitter 内存泄漏.添加了 11 个侦听器

避免在弹性 beantalk 中重建 node_modules

nodejs - 临时文件名

在浏览器中打开来自 Electron 的链接