Skip to content

Inconsistent typing of endless generators between functions and lambdas #63970

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

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<number, void, unknown>' is not assignable to type '() => Generator<number, never, unknown>'.
//  Call signature return types 'Generator<number, void, unknown>' and 'Generator<number, never, unknown>' are incompatible.
//    The types returned by 'next(...)' are incompatible between these types.
//      Type 'IteratorResult<number, void>' is not assignable to type 'IteratorResult<number, never>'.
//        Type 'IteratorReturnResult<void>' is not assignable to type 'IteratorResult<number, never>'.
//          Type 'IteratorReturnResult<void>' is not assignable to type 'IteratorReturnResult<never>'.
//            Type 'void' is not assignable to type 'never'.(2322)

🙁 Actual behavior

Despite both having the same body, the function test has the type Generator<number, void, unknown>, yet the variable yieldsAlways has the type Generator<number, never, unknown>. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions