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

return incorrectly considered to be yielded #22298

Closed
ghost opened this issue Mar 2, 2018 · 3 comments
Closed

return incorrectly considered to be yielded #22298

ghost opened this issue Mar 2, 2018 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@ghost
Copy link

ghost commented Mar 2, 2018

TypeScript Version: 2.8.0-dev.20180302

Code

function* f() {
    yield 0;
    return "";
}

for (const x of f()) {
    const n: number = x;
}

Expected behavior:

No error.

Actual behavior:

src/a.ts(7,11): error TS2322: Type '"" | 0' is not assignable to type 'number'.
  Type '""' is not assignable to type 'number'.
@ghost ghost added Bug A bug in TypeScript Fixed A PR has been merged for this issue labels Mar 2, 2018
@DanielRosenwasser
Copy link
Member

See #22299 (comment)

The feature is working as intended. The returned type becomes the value of an IteratorResult when done is true.

@ghost ghost closed this as completed Mar 2, 2018
@ghost ghost added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Bug A bug in TypeScript Fixed A PR has been merged for this issue labels Mar 2, 2018
@ghost
Copy link
Author

ghost commented Mar 2, 2018

Actually, then shouldn't we do the reverse?

function* f(): IterableIterator<number> {
    return "";
}

Currently this is not an error, but if you removed the type annotation, the return type would be inferred as IterableIterator<string>.

@ghost ghost reopened this Mar 2, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 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

2 participants