microsoft / microsoft/TypeScript

Inconsistent typing of endless generators between functions and lambdas

Aperta
#63,970 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

### 🔎 Search Terms

inconsistent generator lambda
inconsistent generator anonymous
inconsistent generator
wrong generator lambda

### 🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about generators

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=6.0.3#code/GYVwdgxgLglg9mAVAAigUwM5QBQEpkDeAUMsgO4AWMANmstlAE4hr4CeMa1AJsgAwBuIgF8iRWlGQcu3DAHlIdALzJQkWAkR5CJKZx78ho8WknSeGAILUyAQzYZkKtdHhJtxUpRp0GzVnoyhiJiROayChDKqJhQQmH6stZ2Dk4xWPEA9JmoFHTUtljI1DBgdBgUcGSOaIyMcIxE2cgAKmwADnQA5NpKAHzIAOJoZYy2UA0APGAgALYARrUANMgAbnAw3CvgANZgVWB9XcgwjvuShRgwAOZgtvO0qHCoHd29A8Oj41MzC8vIZVW-12+zIhy6ADomjlkABhWzUajIK63cYgRh0DFQdFgF6dRxdT61b6MaZzRaMFbrTbbMB7A5HZC2MC8QkjYkTUm-CkrQHAumg8FMjEnSBwWbtcYwB5oKHNUitPJ4zDILE4tC8eZsZBdMoADxwEKNuGOthFpQg4slsBlyEWUDIaBGuUwdCgrwwcphCtarx1AEl0GNOQAlTAgahQMl-SlrDbcRmnAFwC4YFF3W0TZUBoMksMYCNR7n-PmMI5en0+tqdHMchph7GMMD5wuTakJ45J85MtM3DOPLPumtdQN1xgtyPRnkAtBAsuQ6GVyvV7qj4P10w4idR9uJs4pnvp+4D55D1e50Obpvb6az2rlxdL5d+rrtzv71NHzOnl+lyHYAAmABmACANwIggA

### 💻 Code

```ts
function* test() {
while (true) yield 0;
}

let yieldsOnce = function*() {
yield 0;
}

let yieldsAlways = function*() {
while (true) yield 0;
}

yieldsOnce = test;

yieldsAlways = test;

// the last line shows error
// Type '() => Generator' is not assignable to type '() => Generator'.
// Call signature return types 'Generator' and 'Generator' are incompatible.
// The types returned by 'next(...)' are incompatible between these types.
// Type 'IteratorResult' is not assignable to type 'IteratorResult'.
// Type 'IteratorReturnResult' is not assignable to type 'IteratorResult'.
// Type 'IteratorReturnResult' is not assignable to type 'IteratorReturnResult'.
// Type 'void' is not assignable to type 'never'.(2322)
```

### 🙁 Actual behavior

Despite both having the same body, the function `test` has the type `Generator`, yet the variable `yieldsAlways` has the type `Generator`. The compiler seems to have understood the `test` function incorrectly, so the test code cannot do `yieldsAlways = test`.

### 🙂 Expected behavior

If the compiler knows that the lambda assigned to `yieldsAlways` never finishes, it should know that about the `test` function as well.
The code should be able to do `yieldsAlways = test`.

### Additional information about the issue

_No response_

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

Inizia eseguendo la riproduzione fornita in TypeScript Playground e confronta i tipi di generatore dedotti per la funzione con nome e la lambda. Traccia l’inferenza del tipo restituito dal generatore usata per queste due forme; il lavoro è completato quando entrambe ricevono tipi restituiti compatibili e l’assegnazione `yieldsAlways = test` non segnala più un errore.

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à
Attiva
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.