More Accurate Array Spread
In pre-ES2015 targets, the most faithful emit for constructs like for
/of
loops and array spreads can be a bit heavy.For this reason, TypeScript uses a simpler emit by default that only supports array types, and supports iterating on other types using the —downlevelIteration
flag.The looser default without —downlevelIteration
works fairly well; however, there were some common cases where the transformation of array spreads had observable differences.For example, the following array containing a spread
[...Array(5)]
can be rewritten as the following array literal
[undefined, undefined, undefined, undefined, undefined]
However, TypeScript would instead transform the original code into this code:
Array(5).slice();
which is slightly different.Array(5)
produces an array with a length of 5, but with no defined property slots.
TypeScript 3.6 introduces a new spreadArrays
helper to accurately model what happens in ECMAScript 2015 in older targets outside of —downlevelIteration
.spreadArrays
is also available in tslib.
For more information, see the relevant pull request.