@@ -40,6 +40,8 @@ type PartialTodo = DeepPartial<Todo>;
4040
4141## ` Required<Type> `
4242
43+ 将类型 T 中所有属性转换为必写
44+
4345``` javascript
4446interface ICount {
4547 count?: number;
@@ -60,6 +62,8 @@ type PartialTodo = required<Todo>;
6062
6163## ` Readonly<Type> `
6264
65+ 将类型 T 中所有属性转换为只读
66+
6367``` javascript
6468interface ICount {
6569 count?: number;
@@ -76,8 +80,71 @@ type ReadonlyTodo = Readonly<Todo>;
7680
7781## ` Record<Keys,Type> `
7882
83+ 将一个类型的所有属性值都映射到另一个类型上并创造一个新的类型,` Keys ` 可以是联合类型、对象、枚举、` string ` 、` number ` 、` symbol `
84+
85+ ``` javascript
86+ type unique = keyof any; // string | number | symbol
87+
88+ type myRecord< K extends keyof any, T > = {
89+ [P in K ]: T ;
90+ };
91+
92+ interface Goods {
93+ id: number;
94+ name: string;
95+ price?: number;
96+ }
97+
98+ const goodsMap: Record <string , Goods > = {};
99+ const goodsList: Goods [] = [
100+ { id: 1 , name: ' apple' },
101+ { id: 2 , name: ' orange' , price: 2 },
102+ ];
103+
104+ goodsList .forEach ((goods ) => {
105+ goodsMap[goods .name ] = goods;
106+ });
107+ ```
108+
109+ ``` javascript
110+ type IHttpMethods = ' get' | ' post' | ' delete' | ' put' ;
111+
112+ interface AxiosRequestConfig {
113+ url?: string;
114+ methods: IHttpMethods;
115+ baseURL?: string;
116+ headers?: any;
117+ params?: any;
118+ data?: any;
119+ timeout?: number;
120+ withCredentials?: boolean;
121+ }
122+
123+ interface IHttpFn {
124+ < T = any> (url: string, config?: AxiosRequestConfig): Promise < T > ;
125+ }
126+
127+ const methods = [' get' , ' post' , ' delete' , ' put' ];
128+ const httpMethods: Record <IHttpMethods , IHttpFn > = methods .reduce (
129+ (previousValue : any , currentValue ) => {
130+ previousValue[currentValue] = (
131+ url: string,
132+ options: AxiosRequestConfig,
133+ ) => {
134+ // const { data, ...config } = options;
135+ // Axios[currentValue] = (url, data, config) => { ... }
136+ };
137+
138+ return previousValue;
139+ },
140+ {},
141+ );
142+ ```
143+
79144## ` Pick<Type, Keys> `
80145
146+ 从类型 T 中挑选出指定属性的类型
147+
81148``` javascript
82149interface Person {
83150 name: ' string' ;
@@ -87,10 +154,16 @@ interface Person {
87154
88155type myPick< T , K extends keyof T > = { [P in K ]: T [P ] };
89156type PickPerson = Pick< Person, ' name' | ' age' > ;
157+ // type OmitAddress = {
158+ // name: string;
159+ // age: number;
160+ // }
90161```
91162
92163## ` Omit<Type, Keys> `
93164
165+ 从类型 T 中排除出指定属性的类型
166+
94167``` javascript
95168let person = {
96169 name: ' 1' ,
@@ -99,5 +172,8 @@ let person = {
99172};
100173type myOmit< T , K extends any> = { [P in Exclude< keyof T , K > ]: T [P ] };
101174type OmitAddress = myOmit< typeof person, ' address' > ;
102-
175+ // type OmitAddress = {
176+ // name: string;
177+ // age: number;
178+ // }
103179```
0 commit comments