Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler produces invalid Array().keys().slice() output #18247

Closed
alextreppass opened this issue Sep 5, 2017 · 2 comments
Closed

Compiler produces invalid Array().keys().slice() output #18247

alextreppass opened this issue Sep 5, 2017 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@alextreppass
Copy link

alextreppass commented Sep 5, 2017

I was looking for a compact way to write a 'range' function and stumbled across this stackoverflow answer.

The spread operator variant compiles successfully but the output is not valid.

TypeScript Version: 2.5.2

Code

// expected output
const expected = [0, 1, 2, 3, 4];

// works in vanilla JS
// breaks in TS
// compiles to `Array(5).keys().slice()` which breaks as keys() returns an Iterator
const withSpread = [...Array(5).keys()];

// works in vanilla JS
// works in TS
// compiles to `Array.from(Array(5).keys())`
const from = Array.from(Array(5).keys());

Expected behavior:
The withSpread variant should produce valid JS that generates [0, 1, 2, 3, 4]

Actual behavior:
Resulting JS throws TypeError: Array(...).keys(...).slice is not a function

@ikatyang
Copy link
Contributor

ikatyang commented Sep 5, 2017

Target es5 -> --downlevelIteration
Target es6 -> --target es6

@alextreppass
Copy link
Author

alextreppass commented Sep 5, 2017

Thank you! The compiler didn't give any warnings - just produced invalid code.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Sep 5, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants