microsoft / microsoft/TypeScript

Inconsistent typing of endless generators between functions and lambdas

Offen
#63,970 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

### 🔎 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_

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne damit, die bereitgestellte TypeScript Playground-Reproduktion auszuführen, und vergleiche die abgeleiteten Generator-Typen für die benannte Funktion und die Lambda-Funktion. Verfolge die Inferenz des Rückgabetyps des Generators, die für diese beiden Formen verwendet wird; fertig ist die Aufgabe, wenn beide kompatible Rückgabetypen erhalten und die Zuweisung `yieldsAlways = test` keinen Fehler mehr meldet.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Aktiv
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.