条件类型中的 TypeScript 类型推断

标签 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>;

如果您将鼠标悬停在 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 是什么,它都必须可以分配给两个参数。由于参数类型分别是 numberstring,我们问:什么类型可以分配给这两个?

好吧,数字和字符串


*更多信息,您可能有兴趣阅读关于co and contra-variance .

关于条件类型中的 TypeScript 类型推断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50263072/

相关文章:

Angular 2 在没有模拟的情况下测试具有可观察对象的组件

Angular 6 - 无法订阅从服务返回的数据

mysql BLOB和TEXT数据类型区别

c - 转换为 uint64 时 int32 或 32 位指针的意外符号扩展

objective-c - 在将属性移动到 .h 文件后,ARC 不允许将 Objective-C 指针隐式转换为 'int *'

java - Eclipse Java 编译器和 OpenJDK 编译器之间的类型推断差异 (Java 8)

angular - Ionic2/Cordova typescript 项目中的 Moment.js 插件

typescript - Firestore delete a field in an object is a field of another object inside an array

generics - 如何使用未出现在第一个参数列表中的类型参数来改进 Scala 的类型推断?

rust - 如何推断函数的返回类型?