节点8的推荐打字稿配置 [英] recommended typescript config for node 8

查看:67
本文介绍了节点8的推荐打字稿配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果要使用节点8编译源,建议为打字稿配置什么?

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

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

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

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

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[]'.

我找到了解决此问题的问题.解决方案是使用lib es7. 我可以覆盖默认库:"lib": ["es7"]

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

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

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,targetlibmodule的最佳配置是什么?

So my question is: What are the best configurations for target, lib and module if I want to use node 8?

推荐答案

从Node.js 8.10.0开始,支持100%的ES2017.如果您知道要定位该版本或更高版本,则最佳配置应如下所示:

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:

  • "module": "commonjs"

Node.js可以添加ES模块,但是现在我们必须坚持使用CommonJS.

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

"target": "es2017"

这告诉TypeScript可以输出具有ES2017中功能的JavaScript 语法.实际上,这意味着它将例如输出async/await而不是嵌入polyfill(__awaiter).

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

"lib": ["es2017"]

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

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

因此,完整的配置为:

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


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

这篇关于节点8的推荐打字稿配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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