While TypeScript does handle down-level compilations, the indexOf
method is not one of them. Even when targeting ECMAScript 5, indexOf
will be included due to its presence in the specification. However, it won't be fixed for you if targeting ECMAScript 3.
It's important to note that down-level compilation relates to transitioning between versions of the standard, rather than enhancing browser compatibility.
Below are two examples illustrating down-level compilations, with output varying based on the target specified using target: "ES5"
flags.
let / const
TypeScript allows block-scoped variables to be used:
const x = 'loaded';
{
const x = 'new value';
}
// loaded
console.log(x);
In JavaScript, the inner variable x
is renamed to x_1
to avoid naming conflicts:
var x = 'loaded';
{
var x_1 = 'new value';
}
// loaded
console.log(x);
Iteration
TypeScript enables the use of the for-of
loop:
const x = "loaded";
for (const char of x) {
console.log(char);
}
JavaScript converts the loop into a less elegant but more compatible for loop:
var x = "loaded";
for (var _i = 0, x_1 = x; _i < x_1.length; _i++) {
var char = x_1[_i];
console.log(char);
}