How can I get the type of a generic function applied to a parameter in TypeScript?
For example, how could I define Apply below?
declare function f<T>(t: T): T extends number ? boolean : object;
type ResForNumArg = Apply<typeof f, number>; // should be boolean
type ResForStringArg = Apply<typeof f, object>; // should be object
If there's no way to do this, one can hack around the problem for specific cases by making a type alias that is a type-level version of the function, like F below:
declare function f<T>(t: T): T extends number ? boolean : object;
type ApplyF<T> = T extends number ? boolean : object;
type ResForNumArg = ApplyF<number>; // boolean
type ResForStringArg = ApplyF<object>; // object
But ApplyF can fall out of sync with f and is annoying to type. Is there a better way?
Update: this seems to be related to https://github.com/microsoft/TypeScript/issues/29043