#How do I get the ReturnType of a function with a generic parameter?

5 messages · Page 1 of 1 (latest)

minor tartan
#

I'd like to make a type function called InstantiateWith1<Fn> which takes a generic function Fn that takes a type argument, passes a 1 to the type argument, and returns the result of what the return type of that function would be if instantiated with 1.

I get stuck when writing the extends clause for Fn. TS says that Fn is not generic. What should I do?

knotty lichenBOT
#
johnfn#0

Preview:ts const genericFn = <T>(a: T): T => { return a } type MyGenericFunctionType = typeof genericFn type WorksFine = ReturnType<typeof genericFn<1>> type InstantiateWith1<Fn> = Fn extends MyGenericFunctionType ? ReturnType<Fn<1>> : false // fails, Type 'Fn' is not generic

atomic bison
#

hkt types aren't possible without awkward workarounds

minor tartan
#

But I can do typeof genericFn<1>, it seems like I should be able to get ts to allow me to do the same thing in the second case? No?

floral isle
#

you maybe can use:

Fn extends MyGenericFunctionType
    ? ReturnType<MyGenericFunctionType<1>>