条件类型中的 TypeScript 类型推断

TypeScript type inference in conditional types

我对以下示例中类型的推断方式感到困惑

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 (交集)。

有谁知道为什么输入参数被推断为交集而不是并集?

这可能不是您要找的答案或解释,但在 docs:

Likewise, multiple candidates for the same type variable in contra-variant positions causes an intersection type to be inferred:

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

TL;DR: 因为无论 I 是什么,它都必须可以分配给 all 函数的参数输入 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 的。该反转是 contra 部分。

因此,如果 T 是某个神秘函数类型 (...args: I[]) => R 的子类型,参数反变告诉我们 I 必须可分配给 [=12= 的参数类型].

因此,T extends (...args: (infer I)[]) => infer R 告诉打字稿推断一些单一类型 I 这样 I 可以用来代替 any 参数T.

因此对于您的类型 X,无论 I 是什么,它都必须可以分配给两个参数。由于参数类型分别是numberstring,我们问:什么类型可以分配给这两个?

嗯,number & string


*更多内容,您可能有兴趣阅读关于co and contra-variance