typescript 节点 8 的推荐打字稿配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48378495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:11:14  来源:igfitidea点击:

recommended typescript config for node 8

node.jstypescript

提问by Xenya

What is the recommended config for typescript if I want to ue the compiled sources with node 8?

如果我想使用节点 8 使用已编译的源代码,推荐的打字稿配置是什么?

most tutorials use the following tsconig.json:

大多数教程使用以下tsconig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs"
  }
}

But now I figured out, that not all available features are supported. For example ['foo'].includes('bar')throws the error: Property 'includes' does not exist on type 'string[]'.

但现在我发现,并非所有可用功能都受支持。例如['foo'].includes('bar')抛出错误:属性 'includes' 在类型 'string[]' 上不存在。

I found an issuethat addresses this problem. The solution is to use the lib es7. I could overwrite the default libs: "lib": ["es7"]

我发现了一个问题,即解决了这个问题。解决方案是使用 lib es7。我可以覆盖默认库:"lib": ["es7"]

But I'm not sure if this is the best config for node 8 - are there more features which are not supported by that lib? are there to much features defined?

但我不确定这是否是节点 8 的最佳配置 - 该库不支持更多功能吗?是否定义了很多功能?

So my question is: What are the best configurations for target, liband moduleif I want to use node 8?

所以我的问题是:什么是最好的配置targetlib并且module如果我想使用节点8?

回答by Linus Unneb?ck

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

从 Node.js 开始8.10.0,100% 支持 ES2017。如果您知道您的目标是该版本或更新版本,则最佳配置如下所示:

  • "module": "commonjs"

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

  • "target": "es2017"

    This tells TypeScript that it's okay to output JavaScript syntaxwith features from ES2017. In practice, this means that it will e.g. output async/awaitinstead of embedding a polyfill (__awaiter).

  • "lib": ["es2017"]

    This tells TypeScript that it's okay to use functions and propertiesintroduced in ES2017 or earlier. In practice, this means that you can use e.g. Array.prototype.includesand String.prototype.padStart.

  • "module": "commonjs"

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

  • "target": "es2017"

    这告诉 TypeScript 可以输出带有 ES2017 特性的JavaScript语法。在实践中,这意味着它将例如输出async/await而不是嵌入 polyfill ( __awaiter)。

  • "lib": ["es2017"]

    这告诉 TypeScript 可以使用ES2017 或更早版本中引入的函数和属性。实际上,这意味着您可以使用 egArray.prototype.includesString.prototype.padStart

The full config would thus be:

因此,完整的配置将是:

{
  "compilerOptions": {
    "lib": ["es2017"],
    "module": "commonjs",
    "target": "es2017"
  }
}


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

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

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

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

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

如果您正在运行 Node.js 10,您可以在此处看到我对 Node.js 10 的类似回答

回答by Robula

I'm sure you've already found this but there is Microsoft's starter template here: https://github.com/Microsoft/TypeScript-Node-Starter

我确定你已经找到了这个,但这里有微软的入门模板:https: //github.com/Microsoft/TypeScript-Node-Starter

Whilst you are still on Node 8.x, keep your moduleset to commonjs, targetcan be es6.

虽然您仍在使用 Node 8.x,但请保持您的module设置为commonjs,target可以是es6

compilerOptions.libonly defines what declarations the compiler uses for compile time checks, it does not affect the output of tsc. In other words, you can use whatever libyou want and not worry that your transpiled code will be any different (this is controlled entirely by compilerOptions.target).

compilerOptions.lib仅定义编译器用于编译时检查的声明,它不会影响tsc. 换句话说,您可以使用任何lib您想要的东西,而不必担心您的转译代码会有所不同(这完全由 控制compilerOptions.target)。

Using es7as a libin your case will be fine and will give you type declarations for ES7 and under.

在您的情况下使用es7as alib会很好,并且会为您提供 ES7 及以下的类型声明。

Array.includesis ES7 (ES2016) and therefore as you've discovered is not part of ES6. You could define your libas; lib: ["es6", "ES2016.Array.Include"]to get around your issue.

Array.includes是 ES7 (ES2016),因此正如您所发现的,它不是ES6. 你可以将你定义lib为;lib: ["es6", "ES2016.Array.Include"]解决您的问题。

回答by orta

There's a page in the TypeScript wikiwhich lists recommended TSConfig settings for various node versions.

TypeScript wiki 中有一个页面,其中列出了各种节点版本的推荐 TSConfig 设置。