Closed as not planned
Description
🔎 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 ofa
(In the example) was[ true, true ]
5.2.0-beta
doesn't fix the issue
⏯ Playground Link
💻 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