Node.js 10的Typescript tsconfig设置?

fou*_*r43 14 compilation node.js typescript

有没有人知道Node.js v10.x需要哪些目标/库来使用没有生成器的内置async/await?我看到很多节点8但没有节点10.

Lin*_*äck 20

As of Node.js 10.0.0, 100% of ES2018 is supported. If you know that you are targeting that version or newer, the optimal config would look like this:

  • "module": "commonjs"

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

  • "target": "es2018"

    This tells TypeScript that it's okay to output JavaScript syntax with features from ES2018. In practice, this means that it will e.g. output object rest/spread properties & async/await syntax instead of embedding a polyfill.

  • "lib": ["es2018"]

    This tells TypeScript that it's okay to use functions and properties introduced in ES2018 or earlier. In practice, this means that you can use e.g. Promise.prototype.finally, Array.prototype.includes and String.prototype.padStart.

The full config would thus be:

{
  "compilerOptions": {
    "lib": ["es2018"],
    "module": "commonjs",
    "target": "es2018"
  }
}
Run Code Online (Sandbox Code Playgroud)

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

  • @VivekMaharajh TypeScript 不会为 `Promise.prototype.finally` 添加 polyfill,因此如果您使用 `"lib": ["esnext"]`,您可能会意外地使用 Node.js 10 中不可用的函数。发布的答案,您仍然可以利用所有新的_语言功能_,但不能利用新的_库函数_,因为它们在您的目标上不可用。 (2认同)

Mat*_*hen 8

根据推荐的打字原稿配置为节点8,--target ES2017被支撑在节点8.10.0和较新的(这将包括节点10),并且它足以通过异步函数来输出,而不将它们转换到发电机.

  • 节点10支持100%的ES2018功能,因此您可以定位https://node.green/#ES2018 (13认同)

归档时间:

查看次数:

4920 次

最近记录:

6 年,3 月 前