microsoft / microsoft/TypeScript

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

Open
#34,984 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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 (https://github.com/microsoft/TypeScript-Website/issues/15).

Related Issues:
#30790 Strongly-typed iterator PR

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the TypeScript 3.7.2 diagnostic from example.ts, comparing the NumberIterator and NumberGenerator return types. Read related issue #30790 for iterator context, then verify that the synchronous generator with a union of Iterator and AsyncIterator compiles without the yield-type-never error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.