While exploring TypeScript, I've come across a scenario that has me stumped.
Is there a way in TypeScript to extract the generic type from the typeof
a generic function?
Take this example of a basic generic function:
function echo<T> (input: T) {
return input;
}
I attempted to extract the generic type using the following method:
type IEchoFn = typeof echo;
Unfortunately, it didn't work as expected:
const echo2: IEchoFn = (input: string) => input;
^^^^^
// Type '(input: string) => string' is not
// assignable to type '<T>(input: T) => T'.
I then tried another approach:
type IEchoFn<T> = (typeof echo)<T>;
Alas, this turned out to be invalid syntax.
So, my question remains: How can one successfully extract a generic type from the typeof
a generic function (or its return value)?