When directing towards ES5 and using the spread operator ...
to convert an Iterator
to an Array
, it prompts the need for the -downlevelIteration
compiler option. Enabling this option allows the spread operators to function without any errors.
I'm curious as to why this specification is necessary. Are there any drawbacks or limitations when enabling it, other than increasing the emitted generated code from tslib?
Here's another example: Creating an array dynamically of size N (e.g., 3):
[...Array(3).keys()] // output: [0, 1, 2]
At times, VS Code displays an error:
https://i.sstatic.net/cSrYT.png
Error message from tsc
:
Type 'IterableIterator' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.
Check out the TypeScript Playground to edit the code and view the error