infer是 TypeScript 中条件类型的一个关键字,主要用于在条件类型中进行类型推断。它允许我们在泛型条件类型中声明一个类型变量,然后从其他类型中推断出这个类型。
基本语法
typescript
typeReturnType = Textends(...args:any[]) => infer R ? R :never;
主要使用场景
1.获取函数返回类型
typescr
// 内置的 ReturnType 实现原理type MyReturnType<T> = T extends (...args: any[]) => infer R ? R : never;function foo(): string { return "hello";}type FooReturn = MyReturnType<typeof foo>; // string
2.获取函数参数类型
typescr
// 获取第一个参数类型type FirstParam<T> = T extends (first: infer P, ...rest: any[]) => any ? P : never;// 获取所有参数类型(元组)type Params<T> = T extends (...args: infer P) => any ? P : never;function bar(x: number, y: string): void {}type BarFirstParam = FirstParam<typeof bar>; // numbertype BarParams = Params<typeof bar>; // [number, string]
3.获取数组/元组元素类型
typescript
typescript
typeArrayElement = Textends(infer U)[] ? U :never;typeStrArrayElement=ArrayElement;// stringtypeNumArrayElement=ArrayElement;// number
4.获取 Promise 的解析类型
typescript
typeUnwrapPromise = TextendsPromise ? U : T;typePromiseString=Promise;typeUnwrapped=UnwrapPromise;// string
实际应用示例
示例1:提取对象值类型
typescr
type ValueOf<T> = T extends { [key: string]: infer V } ? V : never;type Obj = { a: number; b: string };type Values = ValueOf<Obj>; // number | string
示例2:提取构造函数实例类型
typescript
typeInstanceType = Textendsnew(...args:any[]) => infer R ? R :any;classAnimal{name:string;constructor(name:string) {this.name= name; }}typeAnimalInstance=InstanceType;// Animal
示例3:递归解包嵌套 Promise
rolex-shanghai.wbiao120.com
rolex-shs.biaoshouhou.cn
rolex-shenzhen.buchererweixiu.com
rolex-shs.hidcwatch.com
rolex-shenzhen.watchgz.cn
rolex-shs.hntwx.cn
rolex-shenzhen.aysza.cn
rolex-shs.watchjwf.cn
rolex-shenzhen.zzjshd.com
rolex-shs.shmwatch.cn
rolex-shenzhen.watch-service.com.cn
rolex-shs.zhcxb.cn
rolex-shs.jshdvip.com
rolex-sys.iwatch4s.com
rolex-shenzhen.jhpwd.cn
rolex-shs.wzjshd.com
rolex-sys.jws-watch.com
rolex-shenzhen.watchwb.cn
rolex-shs.watch-hdl.com
rolex-whs.hdl-watch.com
rolex-shenzhen.watchhdli.cn
rolex-shs.watchrhf.cn
rolex-whs.jshdsh.com
rolex-shenzhen.watchda.cn
rolex-shs.csjshd.com
rolex-gebs.jshdkm.com
rolex-hzs.xajshdzb.com
rolex-shs.watch51.com
rolex-gebs.jshdcq.com
rolex-hzs.watchjt.com
rolex-shs.jshdsx.com
rolex-wxs.guoshew.com
rolex-wxs.jsddshwx.com
rolex-hzs.hljjshd.com
rolex-tys.watchsc.com
rolex-wxs.hbwatch.cn
rolex-hzs.watchwx5.com
rolex-hks.watchwd.com
rolex-qds.gzomegawatch.com
rolex-njs.watchlj.cn
rolex-nts.watchk1.top
rolex-qds.watchjwi.cn
rolex-njs.watchjwd.cn
rolex-cqs.watchjwb.cn
rolex-fss.watchae.com
rolex-njs.szwatchpg.com
rolex-dls.swatchstar.top
rolex-fss.shrolexwatch.com
rolex-njs.rogerweixiu.com
rolex-sz.vay.net.cn
typescript
typeDeepUnwrapPromise =TextendsPromise ?DeepUnwrapPromise : T;typeNestedPromise=Promise>;typeResult=DeepUnwrapPromise;// string
使用注意事项
1.只能用在条件类型的 extends 子句中
typescr
// ✅ 正确type Example1<T> = T extends Array<infer U> ? U : never;// ❌ 错误:infer 不能单独使用type Example2<T> = infer U; // 编译错误
2.多个 infer 可以同时使用
typescr
type Zip<T, U> = T extends [infer A, ...infer RestT] ? U extends [infer B, ...infer RestU] ? [[A, B], ...Zip<RestT, RestU>] : [] : [];type Result = Zip<[1, 2], ['a', 'b']>; // [[1, 'a'], [2, 'b']]
3.协变位置 vs 逆变位置
typescript
// infer 在协变位置(返回类型)typeGetReturnType = Textends() => infer R ? R :never;// infer 在逆变位置(参数类型)typeGetArgType = Textends(arg: infer A) =>any? A :never;
实战技巧
1.类型守卫与 infer
typescr
function isPromise<T>(value: any): value is Promise<T> { return value && typeof value.then === 'function';}async function handle<T>(input: T | Promise<T>): Promise<T> { if (isPromise(input)) { // 这里 input 被推断为 Promise<T> return input; } return Promise.resolve(input);}
2.构建实用类型工具
typescr
// 提取所有方法的返回类型type MethodsReturnType<T> = { [K in keyof T]: T[K] extends (...args: any[]) => infer R ? R : never;};// 提取特定属性的类型type PropType<T, K extends keyof T> = T extends { [P in K]: infer V } ? V : never;
总结
infer的核心作用:在条件类型中提取未知类型的内部结构。它是 TypeScript 类型系统的高级特性,常用于:
类型提取:从已有类型中提取部分类型信息
类型转换:将一个类型转换为另一种形式
类型推导:根据上下文推导出具体类型
工具类型创建:构建复杂的实用类型工具
掌握infer的关键是多实践,从简单的函数返回类型提取开始,逐步应用到更复杂的类型操作中。