The latest addition of ReturnType
in TypeScript 2.8 is a highly valuable feature that enables you to capture the return type of a specific function.
function foo(e: number): number {
return e;
}
type fooReturn = ReturnType<typeof foo>; // number
Nevertheless, I am encountering difficulties when trying to utilize it within the scope of generic functions.
function foo<T>(e: T): T {
return e;
}
type fooReturn = ReturnType<typeof foo>; // type fooReturn = {}
type fooReturn = ReturnType<typeof foo<number>>; // syntax error
type fooReturn = ReturnType<(typeof foo)<number>>; // syntax error
Is there a method to extract the return type of a generic function given certain type parameters?