如何在 TypeScript 中获取函数应用程序的类型?

How can I get the type of a function application in TypeScript?

如何在 TypeScript 中获取应用于参数的泛型函数的类型?

例如,我如何定义下面的Apply

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

playground link

如果没有办法做到这一点,可以通过创建一个类型别名来解决特定情况下的问题,该别名是函数的类型级别版本,如下面的 F 所示:

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

但是 ApplyF 可能会与 f 不同步并且打字很烦人。有没有更好的方法?

Update: this seems to be related to https://github.com/microsoft/TypeScript/issues/29043

正如您正确地发现的那样,不能将函数声明用作泛型类型,如果不执行函数就不可能应用泛型。我们只能在函数调用期间应用泛型(或者将从参数推断):

const r1 = f<number>(1) //  boolean
const r2 = f(1) // boolean

好的,我们知道这是不可能的。现在的解决方法是,为了让它在不失去与原始声明的联系的情况下工作,我建议使用额外的泛型类型 FType。考虑:

type FType<T> = (t: T) => T extends number ? boolean : object;
// join function declaration with FType:
declare function f<T>(...t: Parameters<FType<T>>): ReturnType<FType<T>>

type ResForNumArg =  ReturnType<FType<number>>;    // bool 
type ResForStringArg = ReturnType<FType<object>>;  // object

通过使用实用程序类型 ParametersReturnType,我将 FType 与函数 f 声明连接起来。它很冗长,但我们最终得到了我们想要的,FType 可以以标准方式应用。

Playground link