ยท new features
What is Downleveling in TypeScript?
Downleveling is the process of converting modern TypeScript code into an older version of JavaScript. This allows developers to target older JavaScript environments that may not support the latest features.
Downleveling refers to the process of transpiling modern TypeScript code into an older version of JavaScript. It allows developers to target older JavaScript runtime environments that may not support the latest features and syntax introduced in newer versions of ECMAScript.
When downleveling is applied, TypeScript's compiler (tsc
) converts your source code into an older version of JavaScript that is compatible with the specified target environment.
Contents
Downleveling Example
In ECMAScript 6 several new features were added like the for-of loop statement. When using an older version of JavaScript, such as ECMAScript 5, this syntax has to be downleveled in order to work.
Consider the following example:
Let's say you have written modern TypeScript code using the for...of
loop:
If your "target" is set to "es6", the generated JavaScript code will adhere to the ES6 (ES2015) specification:
Downleveling to ES5
If you set your "target" to an older version of JavaScript, such as ES5, the TypeScript compiler will downlevel your code and emit a traditional for loop instead of the modern for...of
loop:
Please take note that the TypeScript compiler only performs downleveling of syntax (recognizable by special characters like =>
, ?
, `
, #
and keywords like class
or static
) but it does not downlevel the API itself. If you need to polyfill API calls, such as Array.prototype.flat introduced in ES2019
, you would require an additional JavaScript compiler like Babel.
Progressive Enhancement with downlevelIteration
To progressively enhance your code for modern browsers while still targeting an ES5 environment, you can enable the downlevelIteration compiler option. This option generates a helper function that checks if modern Symbol.iterator property is present to support modern loops and iteration. If it's not, the emitted code will fallback to a legacy for
loop: