我对以下示例中类型的推断方式感到困惑
type RetType<T> = T extends (...args: (infer I)[]) => infer R ? [I, R] : any;
type X = (a: number, b: string) => void;
type Q = RetType<X>;
如果您将鼠标悬停在 playground 中的 Q
类型上,您将获得 [number & string, void]
。这很令人困惑,因为我希望 I
被推断为 number | string
(并集)而不是 number & string
(交集)。
有人知道为什么输入参数被推断为交集而不是并集吗?
最佳答案
TL;DR:因为无论 I
是什么,它都必须可以分配给 所有 函数类型 T 的参数
。
这是因为函数参数是反变。这只是意味着要用一个函数代替另一个函数,它的参数类型必须与另一个函数相同或更通用。当您看一个示例时,这是非常明显的:
type f: (arg: string) => string;
type g: (arg: "foo") => string;
// f is assignable to g, since a function expecting
// to receive any string should have no problem accepting
// the specific string "foo".
// However, the reverse isn't true. You can't assign g to f,
// since g expects to receive exactly the string "foo" for its
// argument, but if it's used in place of f, it can receive any string.
换句话说,f
可以分配给 g
因为 g
的参数可以分配给 f
'秒。这种逆转是对立部分。
因此,如果 T
是某个神秘函数类型 (...args: I[]) => R
的子类型,则参数反方差告诉我们 I
必须可分配给 T
的参数类型。
因此,T extends (...args: (infer I)[]) => infer R
告诉 typescript 推断一些单一类型 I
这样 I
可以用来代替T
的任何 参数。
因此对于您的类型 X
,无论 I
是什么,它都必须可以分配给两个参数。由于参数类型分别是 number
和 string
,我们问:什么类型可以分配给这两个?
好吧,数字和字符串
。
*更多信息,您可能有兴趣阅读关于co and contra-variance .
关于条件类型中的 TypeScript 类型推断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50263072/