Skip to content

Generator functions which have a return type of a union of Iterator/AsyncIterator have a yield type of never #34984

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

Open
brainkim opened this issue Nov 7, 2019 · 1 comment
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@brainkim
Copy link

brainkim commented Nov 7, 2019

TypeScript Version: 3.7.2

Search terms: generator iterator asynciterator type union never

Code

example.ts

type NumberIterator = Iterator<number> | AsyncIterator<number>;
type NumberGenerator = Generator<number> | AsyncGenerator<number>;

function *numbers(): NumberIterator {
    yield 1;
}

A type alias which is a union of Iterator and AsyncIterator is used as the return type of a synchronous generator.

Expected behavior:

Code compiles.

Actual behavior:

example.ts(5,11): error TS2322: Type '1' is not assignable to type 'never'.

If you replace NumberIterator with NumberGenerator as the return type the function above, the code correctly type-checks. Something is wrong here.

Playground Link:

https://www.typescriptlang.org/play/index.html?target=99&ssl=5&ssc=2&pln=1&pc=1#code/C4TwDgpgBAcgrgWwEYQE4ElhoIbAPapQC8UmO+qAPAHaIqoB8UAPlAIIDOI1AxmargI06aBgG4AUBIBmcXsACWealABUtZGg4AKAJQAuWCIxYBFKAG8JUG1BAKIAGwAmUAIySAvkA

The playground link won’t work because the playground doesn’t support async iterators yet (microsoft/TypeScript-Website#15).

Related Issues:
#30790 Strongly-typed iterator PR

@brainkim brainkim changed the title Generator functions which return a union of Iterator/AsyncIterator have a yield type of never Generator functions which have a return type of a union of Iterator/AsyncIterator have a yield type of never Nov 7, 2019
@andrewbranch andrewbranch added the Needs Investigation This issue needs a team member to investigate its status. label Dec 6, 2019
@brainkim
Copy link
Author

The playground example now correctly shows the error in 4.0.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

2 participants