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

Poor typing for array iterator #34581

Closed
pauldraper opened this issue Oct 18, 2019 · 5 comments
Closed

Poor typing for array iterator #34581

pauldraper opened this issue Oct 18, 2019 · 5 comments
Assignees
Labels
Duplicate An existing issue was already created

Comments

@pauldraper
Copy link

pauldraper commented Oct 18, 2019

TypeScript Version: 3.6.3

Search Terms: Array iterator

Code

const array = [1];
const iterator = array[Symbol.iterator]();
const value: string = iterator.next().value; // compile error

Expected behavior:

A type check error that number cannot be string.

Actual behavior:

No type check error.

Playground Link: https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=4&pc=1#code/MYewdgzgLgBAhgJwXAnjAvDA2gRgLoDcAsAFCiSwCWUApslCAhvEqlgMooC2ARiADYA6anTgMEeABQBKYmXDQYANzj8ArjQBcMaAkpgA5sxH1GgsDQAeUGYJXqaBGAHpnMUFwAOlfjRh0ERlIgA

Related Issues:

I do not that Iterators/Generators were changed recently. IDK if that is related.

@AviVahl
Copy link

AviVahl commented Oct 18, 2019

You'll get correct type error if you verify that !result.done:

const array = [1];
const iterator = array[Symbol.iterator]();
const res = iterator.next()
if (!res.done) {
    const num: string = res.value
}

@fatcerberus
Copy link

Cause of this is that IterableIterator<number> is an alias for Iterator<number, any, undefined> (thanks to the changes to iterator typings in 3.6) so without checking the value of done first, value is typed as number | any, which collapses to just any.

@AviVahl
Copy link

AviVahl commented Oct 19, 2019

I wonder whether it would have been possible to use unknown as default for the iterator's TReturn type..? @rbuckton might be able to clarify.

@AviVahl
Copy link

AviVahl commented Oct 20, 2019

Oh, see discussion at #33353

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Oct 30, 2019
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.8.0 milestone Oct 30, 2019
@RyanCavanaugh RyanCavanaugh added the Rescheduled This issue was previously scheduled to an earlier milestone label Aug 31, 2020
@rbuckton
Copy link
Member

This is essentially a duplicate of #33353.

@rbuckton rbuckton added Duplicate An existing issue was already created and removed Needs Investigation This issue needs a team member to investigate its status. Rescheduled This issue was previously scheduled to an earlier milestone labels Dec 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants