This is because the --target
(or just -t
) by default will convert to ES3, very similar to ES5, but has many Apis that do not exist in it (although in this case depends on the browser) and will not use the strict mode
which is something of ES5, as it is said in the documentation:
The use of ES3 as a standard is probably to try to be as backward as possible, of course this back and forth may imply some unwanted effect.
If you believe you will use Typescript only in environment that will support ES6+ then just specify to convert to the desired, remembering that let
is not ES5, so if you want to use exactly how it is just adjust in the compiler:
tsc meuscript.ts -t ES6
The values supported by --target
(-t
) sane:
- ES3 (standard)
- SE5
- ES6 or ES2015
- ES2016
- ES2017
- ES2018
- ES2019
- ES2020
- Esnext (these are all the latest features on https://github.com/tc39/proposals, which of course there are no guarantees to run in all environments, since this is to "convert")
So that’s the reason, the default for now is the ES3, but you have full control to adjust to what suits you.
Typescript also has many supported environments and each works in one way, so I’ll try to summarize, the tsconfig.json
is the configuration file in most projects that use Typescript, with it you can configure in it how you want it to be conversion, example including some libs and conversion to ES2019:
{
"$schema": "https://json.schemastore.org/tsconfig",
...
"compilerOptions": {
"lib": ["es2019", "es2020.promise", "es2020.bigint", "es2020.string"],
"module": "commonjs",
"target": "es2019",
...
}
}
Documentation: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
It is probably because of the version of the destination Ecmascript.
– Jéf Bueno
I was thinking of leaving Babel to Tsc in Node, does this behavior happen with the latest versions of Ecmascript? 'Cause it’s become a pattern that I don’t use var in nodejs.
– Chance