前言
在日常工作中,有时为了满足特殊的需求,需要定制一些自定义工具类型,以达到提高工作效率及简化代码的目的。本篇文章将记录几种自定义工具类型的使用及实现,以便学习。
更多自定义工具可以参考 utility-types
1.Proxy
namespace a {
type Proxy<T> = {
get(): T,
set(value: T): void
}
type Proxity<T> = {
[P in keyof T]: Proxy<T[P]>;
}
function proxity<T>(obj: T): Proxity<T> {
let result = <Proxity<T>>{};
for (const key in obj) {
Object.defineProperty(result, key, {
enumerable: true,
configurable: true,
get: () => {
return obj[key];
},
set: (value) => {
obj[key] = value;
}
})
}
return result;
}
interface Props {
name: string,
age: number
}
let obj: Props = {
name: '张三',
age: 10
}
obj.name = '李四'
let res = proxity<Props>(obj);
console.log(res); // { name: [Getter/Setter], age: [Getter/Setter] }
}
2.unProxy
namespace a {
type Proxy<T> = {
get(): T,
set(value: T): void
}
type Proxity<T> = {
[P in keyof T]: Proxy<T[P]>;
}
function unProxity<T>(t: Proxity<T>): T {
let result: any = {} as T;
for (const key in t) {
result[key] = t[key];
}
return result;
}
let ret = unProxity<Props>(res);
console.log(ret); // { name: '李四', age: 10 }
}
3.SetDifference
namespace b {
type SetDifference<T, U> = T extends U ? never : T;
type A = string | number | symbol;
type B = number | boolean;
type AB = SetDifference<A, B>; // type AB = string | symbol
}
4.Omit
- 忽略
- 忽略某些属性,返回剩余的属性
- keyof T 是name | age | isMan
- K 是传递进来的 ‘age’ | ‘name’
- SetDifference就相当于从 name | age | isMan中找出不含 age | name的属性
- 通过 Pick 返回筛选后的属性,即 { isMan: boolean; }
namespace c {
type SetDifference<T, U> = T extends U ? never : T;
type Omit<T, K extends keyof any> = Pick<T, SetDifference<keyof T, K>>;
type Person = { name: string, age: number, isMan: boolean };
type OmitAgePerson = Omit<Person, 'age' | 'name'>; // { isMan: boolean; }
}
5.Diff
namespace d {
type Props = { name: string, age: number, isMan: boolean };
type DefaultProps = { age: number };
type SetDifference<T, U> = T extends U ? never : T;
type Diff<T extends object, U extends object> = Pick<T, SetDifference<keyof T, keyof U>>;
type DiffProps = Diff<Props, DefaultProps>; // { name: string, isMan: boolean; }
}
6.InterSection
- 交集
- 返回 T 和 U 中共有的属性
namespace e {
// 注意:下面这三种写法都可以
// Extract<keyof T, keyof U> & Extract<keyof U, keyof T>
// Extract<keyof T, keyof U>
// Extract<keyof U, keyof T>
type InterSection<T extends object, U extends object> = Pick<T, Extract<keyof T, keyof U> & Extract<keyof U, keyof T>>;
type Props = { name: string, age: number, isMan: boolean };
type DefaultProps = { age: number };
type DuplicateProps = InterSection<Props, DefaultProps>; // { age: number }
}
7.OverWrite
namespace f {
type OldProps = { name: string, age: number, isMan: boolean };
type NewProps = { name: number, sex: string };
// SetDifference 找出 T 中有,但 U 中没有的属性
type SetDifference<T, U> = T extends U ? never : T;
// Diff 找出 T 中有,但 U 中没有的属性
type Diff<T extends object, U extends object> = Pick<T, SetDifference<keyof T, keyof U>>;
// InterSection 找出 T 和 U 中都有的属性
type InterSection<T extends object, U extends object> = Pick<T, Extract<keyof T, keyof U> & Extract<keyof U, keyof T>>;
type OverWrite<
T extends object,
U extends object,
// Diff<T, U> 是:{ age: number, isMan: boolean }
// InterSection<U, T> 是:{ name:number }
// 所以 I 就是:{ name:number, age: number, isMan: boolean }
// 如果也想返回 sex 属性,那么此处需要改写成 I = Diff<T, U> & U
I = Diff<T, U> & InterSection<U, T>
> = Pick<I, keyof I>;
type ReplaceProps = OverWrite<OldProps, NewProps>; // { name: number, age: number, isMan: boolean };
}
8.Merge
namespace g {
type Obj1 = {
id: number,
name: string
}
type Obj2 = {
id: number,
age: number
}
type Compute<T extends any> = T extends Function ? T : { [K in keyof T]: T[K] };
type SetDifference<T, U> = T extends U ? never : T;
type Omit<T, K extends keyof any> = Pick<T, SetDifference<keyof T, K>>;
type Merge<T extends object, U extends object> = Compute<Obj1 & Omit<Obj2, keyof Obj1>>;
type obj = Merge<Obj1, Obj2>;
/**
type obj = {
id: number;
name: string;
age: number;
}
*/
}
总结
原文地址:https://blog.csdn.net/to_the_Future/article/details/126920675
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_7005.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。