Repository navigation
Can't infer return type of generic function #55435
Copy link
Copy link
Closed as not planned
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
🔎 Search Terms
infer generic function return type
🕗 Version & Regression Information
- Before version
3.5.1the syntax wasn't supported - Before and during version
4.8.4there was an additional bug for which the type ofa(In the example) was[ true, true ] 5.2.0-betadoesn'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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Activity
jcalz commentedon Aug 19, 2023
TS doesn't have true call types of the sort you'd need for this to work.
Looks like a duplicate of #52035 (or at least this comment) and refers back to #6606.
AFatNiBBa commentedon Aug 20, 2023
I think this is something that has to be looked into because it has great potential and it almost works.
Consider the following:
It understands what the return type is, so why can't it infer it
fatcerberus commentedon Aug 20, 2023
It seems like you want specifically #40179
jcalz commentedon Aug 20, 2023
Yeah #40179 was the one I was searching for but "call types" wasn't mentioned there
steveluscher commentedon Aug 22, 2023
I'm also blocked from making one of my more complicated types generic because of this. The
satisfieskeyword is able to expand all of the possible return types ofdoThingbelow, so can we makeinferdo the same?Output
Compiler Options
{ "compilerOptions": { "strict": true, "noImplicitAny": true, "strictNullChecks": true, "strictFunctionTypes": true, "strictPropertyInitialization": true, "strictBindCallApply": true, "noImplicitThis": true, "noImplicitReturns": true, "alwaysStrict": true, "esModuleInterop": true, "declaration": true, "target": "ES2017", "jsx": "react", "module": "ESNext", "moduleResolution": "node" } }Playground Link: Provided
refactor(experimental): reduce `getProgramAccounts` types using generics
typescript-bot commentedon Aug 25, 2023
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.