Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript tsconfig settings for Node.js 10?

Does anyone know which target/libs are required for Node.js v10.x to use the built in async/await without the generators? I see a lot for node 8 but not with node 10.

like image 756
four43 Avatar asked Aug 06 '18 22:08

four43


People also ask

Does ts node use Tsconfig json?

ts-node automatically finds and loads tsconfig. json . Most ts-node options can be specified in a "ts-node" object using their programmatic, camelCase names. We recommend this because it works even when you cannot pass CLI flags, such as node --require ts-node/register and when using shebangs.

What is Tsconfig json in node JS?

The tsconfig. json file specifies the root files and the compiler options required to compile the project. JavaScript projects can use a jsconfig. json file instead, which acts almost the same but has some JavaScript-related compiler flags enabled by default.

Can I use TypeScript with node js?

TypeScript is well-established in the Node. js world and used by many companies, open-source projects, tools and frameworks. Some of the notable examples of open-source projects using TypeScript are: NestJS - robust and fully-featured framework that makes creating scalable and well-architected systems easy and pleasant.


2 Answers

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 its 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"
  }
}

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

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 8 you can see my similar answer for Node.js 8 here

like image 169
Linus Unnebäck Avatar answered Oct 19 '22 09:10

Linus Unnebäck


According to recommended typescript config for node 8 , --target ES2017 is supported on Node 8.10.0 and newer (which would include Node 10), and it is sufficient to pass through async functions to the output without translating them to generators.

like image 7
Matt McCutchen Avatar answered Oct 19 '22 08:10

Matt McCutchen