Reputation: 7294
TypeScript's has a target
configuration with many values, like esnext
, es2015
, es6
etc. (very confusing)
NodeJs current version (11.11.0) supports many of the new features of JavaScript. Is it ok to target esnext
on TypeScript? will it work?
If not, what's the right target
to use when targeting nodeJs 11.11.0
Edit:
Thanks to @Seblor we know that esnext
is very dynamic and TC39 can add features as they see fit. It represents the next version of JavaScript that is being worked on (Regarding agreeing on features)
The refined question should be: According to the current version of NodeJs (11.11.0) and the current version of TypeScript (3.3) can we use esnext
as the target
?
Upvotes: 19
Views: 9734
Reputation: 7535
Based on Node-Target Mapping on the TypeScript wiki "es2018"
is the right mapping for Node 10, "es2019"
is the right mapping for Node 12. It seems that non-LTS versions of Node aren't documented, but given this information I feel like "es2018"
is the safest target.
Upvotes: 10
Reputation: 7294
Ok so the answer is (As usual) Depends!
It depends on the features that you plan on using. The TypeScript version, target
on the tsconfig.json
and the NodeJS version.
There are two things to check before you can use a JavaScript feature:
You have to look at this table, and look at your target environment column, in my case it's NodeJs column. And make sure that the cell is Green. Red means that you can't run this feature on this environment.
Make sure that you can compile your code using TypeScript. If you can compile the code using TypeScript, you're ok. Note that you can NOT count on the TypeScript column. Red cell means that the compiled code will not run on previous version of JavaScript...
For example BigInt
:
TypeScript with target: "esnext"
will compile (With no problem) a code with bigint
(While the support cell is Red), and you can run a JavaScript code with bigint on NodeJS 11
Note that on the top left, there's the ECMAScript
version-group. you can go lower than esnext
Upvotes: 3
Reputation: 7136
Looking at node.green, ES2018 support seems to be full in Node 11.11.0, so you should be able to set ES2018 as target with no trouble.
You can push your luck by using ES2019 or even ESNext since the support looks good enough. However, I do not know if typescript will use non-yet-implemented features if you use ESNext as target (like the private properties).
Personally I would stick with ES2018, as I see no need to push for ESNext.
Upvotes: 5