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

Cant iterate over Array.prototype.entries inside for of loop #35080

Closed
n-filatov opened this issue Nov 13, 2019 · 4 comments
Closed

Cant iterate over Array.prototype.entries inside for of loop #35080

n-filatov opened this issue Nov 13, 2019 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@n-filatov
Copy link

I have a problem to iterate inside for of loop over array.entries(). Compiled version of code trys to get length property of array.entries but this property is undefined, coz array.entries returns Iterator which has not length property.
Code

const a: any = [1, 2, 3];

for (const x of a.entries()) {
    console.log(x)
}

Expected behavior:
Code will iterate over a.entries and call console.log()

How it works in babel: Babel Playground

Actual behavior:
Nothing happens, coz no iteration.

Playground Link: Playground

@nmain
Copy link

nmain commented Nov 13, 2019

This is working as intended. If you want full iterable semantics in downlevel emit, turn on the downlevel iteration compiler flag: https://www.typescriptlang.org/docs/handbook/compiler-options.html

@n-filatov
Copy link
Author

It is ok that my code wont works properly by default and i should use flags for it. I thought that when i set target as ES5 it will compile working code by default.

@nmain
Copy link

nmain commented Nov 13, 2019

I believe this has been discussed before, but I couldn't find much in search. In ##34572 @DanielRosenwasser states "We have opinionated defaults for downlevel iteration." as a side comment but doesn't elaborate further.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Nov 15, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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

4 participants