Skip to content

Can't infer return type of generic function #55435

Closed as not planned
Closed as not planned
@AFatNiBBa

Description

@AFatNiBBa

🔎 Search Terms

infer generic function return type

🕗 Version & Regression Information

  • Before version 3.5.1 the syntax wasn't supported
  • Before and during version 4.8.4 there was an additional bug for which the type of a (In the example) was [ true, true ]
  • 5.2.0-beta doesn't fix the issue

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.1.6#code/FAehAIHUHsCcGsDO5C8G4WR3gG9jl+ALgJ4AOApuAMIAWpAxvADwAqANOAArikAe+pAdgBNkACm4AucAEN+hAJTgAvAD5wAI2jQANqRmrFHLrwHDwYyUwUqCsAK7kA-DfvhJAMylbEpANzAceGBQcEjgAO4AlvhU4LTQALbEOtxGxLCkiIgR0PyIAbhEZOAAkogActD4AIzM+pQ09MxsDACqyubgLVaqLUZ8QshV4E4eXuSS+Hakyn54BCTkUkrgANol5ZU1AEzKbKUV1QxVqgC6s4EQuAB6Dv5zQTAIyOn4trD8EfwA5gQ04J-EWz4fLzIqlJhTWrLah0RiscC1DqWJSqJgzEGFchqZZrcGQyb2XbrCH2Bijbync64ILXW4AX38QQAItAMvwAOT4cIhMwAIVsP2IUlg+DkWBBzNZiA5XPg-GgYX++GQAKBGIWlE8WgYADE+iZkDrbPxaPhsvw2ABBWBfZA8fqmdJSQQ5LSEcDGuUK-grE51PX2g1mAB0oeFtsk1tt3X+-DcpFgnWGSck-FIADcE1TQeRaNCtQxERJwMjrPC1kM-RLLuAbncLuADlxM-xwDlpG20wBaIWwKTxUh8RNZRJaCJuCKkQTgTOwLI5dVFChamoB4wDcBGk1mnJsNHLNcO0TF0uqT7xxO9Jy9VMZrOL8jTgzLrRaGpFiwx+HHat4Ou-lk2U5bkEHCKIYjiUceFSdJMnNPI5kxLh81fd80SRGM1nhLZwBONgf3uGt-0I4InnCGhW2iBNFnSGVwC+AQEwiWgELwJDShqfdnwLDoqhjAlSDYLDcPRRCNTcHF1hqASiQ4o5KV-WlgDpIA

💻 Code

// Works 👍
{
    type Check<T, P extends (x: any) => boolean> = P extends (x: T) => true ? true : false;

    // Works with complex expressions
    type IsNot1<T> = Check<T, <U>(x: U) => U extends 1 ? false : true>;
    type a = [ IsNot1<2>, IsNot1<1> ];
    //   ^? type a = [ true, false ]

    // Works returning the input
    type IsTrue<T> = Check<T, <T>(x: T) => T>;
    type b = [ IsTrue<true>, IsTrue<false> ];
    //   ^? type b = [ true, false ]
}

// Doesn't work (Bug part)
{
    // Doesn't know its input
    type Call<F extends Function, Args extends readonly unknown[]> = F extends (...args: Args) => infer U ? U : never;
    type c = Call<<T>(x: T) => T, [ 1 ]>
    //   ^? type c = unknown

    // Not even on a one-parameter simplified version
    type Call1<F extends Function, T> = F extends (x: T) => infer U ? U : never;
    type d = Call1<<T>(x: T) => T, 1>
    //   ^? type d = unknown

    // Doesn't work with complex expressions
    type e = Call1<<T>(x: T) => [ T, 2 ], 1>
    //   ^? type e = [ unknown, 2 ]

    // Works when there aren't generics
    type Is1<T> = Call<(x: 1) => true, [ T ]>;
    type f = [ Is1<true>, Is1<1> ];
    //   ^? type f = [ never, true ]
}

🙁 Actual behavior

Types c, d and e[0] are unknown

🙂 Expected behavior

Types c, d and e[0] should have all been 1.
I tried to infer the return type of some generics functions when they got supplied with certain types, I can check wether the return type is something else (true inside Check in this example) but I cannot actually get it

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions