分享几个我工作中封装的typeScript方法

TS目前支持的方法已经有不少了https://www.typescriptlang.or...
但是还是不够,下面分享几个我自己封装的常用方法
交集

type Intersection = Pick< T, Extract & Extract >;

分享几个我工作中封装的typeScript方法
文章图片

差集
type Diff = Omit< T & U, keyof Intersection >;

分享几个我工作中封装的typeScript方法
文章图片

将指定属性变为Optional
type PartialKey = Partial & Omit;

分享几个我工作中封装的typeScript方法
文章图片

将指定属性变为Required
type RequiredKey = U & Required>;

分享几个我工作中封装的typeScript方法
文章图片

获取数组元素类型
type ArrayItem = T extends (infer P)[] ? P : never;

分享几个我工作中封装的typeScript方法
文章图片

获取Record中value类型
type RecordValueType = U extends Record ? P : never;

分享几个我工作中封装的typeScript方法
文章图片

获取Promise返回类型
type PromiseReturnType> = U extends Promise ? P : never;

分享几个我工作中封装的typeScript方法
文章图片

【分享几个我工作中封装的typeScript方法】代码: https://stackblitz.com/edit/t...

    推荐阅读