microsoft / microsoft/TypeScript

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

Aperta
#34,984 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Needs Investigation
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci la diagnostica di TypeScript 3.7.2 da example.ts, confrontando i tipi restituiti da NumberIterator e NumberGenerator. Leggi la issue #30790 correlata per il contesto degli iteratori, quindi verifica che il generatore sincrono con un’unione di Iterator e AsyncIterator venga compilato senza l’errore yield-type-never.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.