microsoft / microsoft/TypeScript

Inconsistent typing of endless generators between functions and lambdas

Open
#63,970 2 comments 0 reactions 0 assignees View on GitHub
Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

Research direction

Start by running the provided TypeScript Playground reproduction and compare the inferred generator types for the named function and lambda. Trace the generator return-type inference used for these two forms; done means both receive compatible return types and the `yieldsAlways = test` assignment no longer reports an 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
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.