Typescript 条件类型中的类型脚本类型推断
在下面的示例中,我对类型是如何推断的感到困惑Typescript 条件类型中的类型脚本类型推断,typescript,types,type-inference,Typescript,Types,Type Inference,在下面的示例中,我对类型是如何推断的感到困惑 type RetType<T> = T extends (...args: (infer I)[]) => infer R ? [I, R] : any; type X = (a: number, b: string) => void; type Q = RetType<X>; type-RetType=T扩展(…参数:(推断I)[])=>推断R?[I,R]:任何; 类型X=(a:数字,b:字符串)=>void;
type RetType<T> = T extends (...args: (infer I)[]) => infer R ? [I, R] : any;
type X = (a: number, b: string) => void;
type Q = RetType<X>;
type-RetType=T扩展(…参数:(推断I)[])=>推断R?[I,R]:任何;
类型X=(a:数字,b:字符串)=>void;
Q型=RET型;
如果您将鼠标悬停在操场上的Q
类型上,您将获得[number&string,void]
。这很混乱,因为我希望I
被推断为number | string
(并集),而不是number&string
(交集)
有人知道为什么输入参数被推断为一个交集而不是一个并集吗?这可能不是您想要的答案或解释,但这在以下部分中被称为: 同样,同一类型变量在反向变量位置的多个候选变量会导致推断交叉点类型:
type Bar<T> = T extends { a: (x: infer U) => void, b: (x: infer U) => void } ? U : never;
type T20 = Bar<{ a: (x: string) => void, b: (x: string) => void }>; // string
type T21 = Bar<{ a: (x: string) => void, b: (x: number) => void }>; // string & number
type Bar=T扩展了{a:(x:inferu)=>void,b:(x:inferu)=>void}?U:从来没有;
类型T20=条形空心,b:(x:string)=>void}>;//一串
T21型=钢筋空心,b:(x:编号)=>void}>;//字符串和数字
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扩展(…args:(推断I)[])=>inferr
告诉typescript推断某个单一类型的I
,这样I
就可以用来代替T
的任何参数
因此,对于您的类型X
,无论I
是什么,它都必须可分配给这两个参数,这应该是正确的。由于参数类型分别是number
和string
,我们会问:哪种类型可分配给这两种类型
嗯,数字和字符串
*更多信息,您可能有兴趣阅读。这很有意义。我一打这个问题就想起函数参数是反变的。