Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

node.js - recommended typescript config for node 8

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

most tutorials use the following 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[]'.

I found an issue that addresses this problem. The solution is to use the lib es7. I could overwrite the default libs: "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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 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 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"]

    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.

The full config would thus be:

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

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

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...