分享几个我工作中封装的typeScript方法
TS目前支持的方法已经有不少了https://www.typescriptlang.or...
但是还是不够,下面分享几个我自己封装的常用方法
交集
type Intersection = Pick<
T,
Extract & Extract
>;
![分享几个我工作中封装的typeScript方法](https://img.it610.com/image/info9/e0d3c9920cfe464d9daedff7076eeae3.jpg)
文章图片
差集
type Diff = Omit<
T & U,
keyof Intersection
>;
![分享几个我工作中封装的typeScript方法](https://img.it610.com/image/info9/658afe0a3fb549f78aee0a0f417ee777.jpg)
文章图片
将指定属性变为Optional
type PartialKey = Partial & Omit;
![分享几个我工作中封装的typeScript方法](https://img.it610.com/image/info9/75b8fce8170043f58cf4c8174840de2f.jpg)
文章图片
将指定属性变为Required
type RequiredKey = U &
Required>;
![分享几个我工作中封装的typeScript方法](https://img.it610.com/image/info9/ce7e2b1791194d849cc3f20919e8c676.jpg)
文章图片
获取数组元素类型
type ArrayItem = T extends (infer P)[] ? P : never;
![分享几个我工作中封装的typeScript方法](https://img.it610.com/image/info9/3a63af5bda724a7aa7ed987b6b5509d7.jpg)
文章图片
获取Record中value类型
type RecordValueType = U extends Record ? P : never;
![分享几个我工作中封装的typeScript方法](https://img.it610.com/image/info9/3c591c1ffada44b1ac72e1c8e73112a7.jpg)
文章图片
获取Promise返回类型
type PromiseReturnType> = U extends Promise ? P : never;
![分享几个我工作中封装的typeScript方法](https://img.it610.com/image/info9/9ef9bbcc5d4c410abe65580276d73f20.jpg)
文章图片
【分享几个我工作中封装的typeScript方法】代码: https://stackblitz.com/edit/t...
推荐阅读
- 我要做大厨
- 一个小故事,我的思考。
- 家乡的那条小河
- 第三节|第三节 快乐和幸福(12)
- 第326天
- 这辈子我们都不要再联系了
- 死结。
- 跌跌撞撞奔向你|跌跌撞撞奔向你 第四章(你补英语,我补物理)
- 我从来不做坏事
- 喂,你结婚我给你随了个红包