本文已参与「新人创作礼」活动,一起开启掘金创作之路。
高级方法
- Partial
- Required
- Readonly
- Record
- Pick
- Omit
- Exclude
- Extract
- NonNullable
- ReturnType
Partial
Partial: 将输入的类型全部转换为可选类型
type _Partial<T> = { [K in keyof T]+?: T[K]; }
interface Todo {
title: string;
description: string;
}
const todo: Todo = { title: '西瓜' } // ERROR Property 'decription' is missing in type '{ title: string; }'
const todo2: Partial<Todo> = { title: '西瓜' }
const todo3: _Partial<Todo> = { title: '西瓜' }
Required
Required: 将输入的类型全部转换为必填类型
type _Required<T> = { [K in keyof T]-?: T[K]; }
interface Todo {
title?: string;
description?: string;
}
const todo: Todo = { title: '西瓜' }
const todo2: Required<Todo> = { title: '西瓜' } // ERROR, Property 'decription' is missing in type '{ title: string; }'
const todo3: _Required<Todo> = { title: '西瓜' } // ERROR, Property 'decription' is missing in type '{ title: string; }'
Readonly
Readonly: 将输入的类型全部转换为只读类型
type _Readonly<T> = { readonly [K in keyof T]: T<K>; }
interface Todo {
title: string;
description?: string;
}
const todo: Todo = { title: '西瓜' }
todo.title = '哈密瓜'
const todo2: Readonly<Todo> = { title: '西瓜' }
todo2.title = '哈密瓜' // ERROR, Cannot assign to 'title' because it is a read-only property
const todo3: _Readonly<Todo> = { title: '西瓜' }
todo3.title = '哈密瓜' // ERROR, Cannot assign to 'title' because it is a read-only property
Record
Record: 将输入的值类型映射到输入的枚举类型中
// 说明一下, keyof any对应的类型为number | string | symbol
type _Record<K extends keyof any, T> = { [P in K]: T; }
interface Person {
age: number;
hobby: string[];
}
type PersonName = "yardam" | "xigua";
const person: Record<PersonName, Person> = {
yardam: { age: 18, hobby: ['读书'] },
xigua: { age: 28, hobby: ['游泳'] }
}
const person: _Record<PersonName, Person> = {
yardam: { age: 18, hobby: ['读书'] },
xigua: { age: 28, hobby: ['游泳'] }
}
Pick
Pick: 从输入的类型中筛选出指定的类型
type _Pick<T, K> = { [T, K extends keyof T]: T[K]; }
interface Todo {
title: string;
description: string;
}
const todo: Todo = { title: '西瓜', description: '保甜' }
const todo2: Pick<Todo, 'title'> = { title: '西瓜' }
const todo3: _Pick<Todo, 'title'> = { title: '西瓜' }
Omit
Omit: 将输入的类型中过滤掉指定的类型
type _Omit<T, K extends keyof any> = { [P in Exclude<keyof T, K>]: T[P]; }
interface Todo {
title: string;
description: string;
}
const todo: Todo = { title: '西瓜', description: '保甜' }
const todo2: Omit<Todo, 'description'> = { title: '西瓜' }
const todo3: _Omit<Todo, 'description'> = { title: '西瓜' }
Exclude
Exclude: 从输入的枚举类型中排除指定的类型
type _Exclude<T, K> = T extends K ? never : T;
type Hobby = 'swim' | 'play football' | 'play tennis' | 'read book';
type Hobby2 = Exclude<Hobby, 'play tennis'>; // 'swim' | 'play football' | 'read book';
type Hobby3 = _Exclude<Hobby, 'play tennis'>; // 'swim' | 'play football' | 'read book';
Extract
Extract: 从输入的枚举类型中筛选指定的类型
type _Extract<T,K> = T extends K ? T : never;
type Hobby = 'swim' | 'play football' | 'play tennis' | 'read book';
type Hobby2 = Extract<Hobby, 'play tennis'>; // 'play tennis';
type Hobby3 = _Extract<Hobby, 'play tennis'>; // 'play tennis';
NonNullable
NonNullable: 从输入的枚举类型中排除 null 和 undefined
type _NonNullable<T> = T extends null | undefined ? never : T;
type Hobby = 'swim' | 'play tennis' | 'read book' | null | undefined;
type Hobby2 = NonNullable<Hobby>; // 'swim' | 'play tennis' | 'read book';
type Hobby3 = _NonNullable<Hobby>; // 'swim' | 'play tennis' | 'read book';
ReturnType
ReturnType: 从输入的符合约束的函数类型中推断出函数执行后的返回类型
type _ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer P ? P : any
type RTE = ReturnType<string>; // ERROR, Type 'string' does not satisfy the constraint '(...args: any) => any'
type RTE2 = ReturnType<Function>;
// ERROR, Type 'Function' does not satisfy the constraint '(...args: any) => any',
// ERROR, Type 'Function' provides no match for the signature '(...args: any): any'.
type RT = ReturnType<() => string> // string
type RT2 = ReturnType<() => void> // void
type RT3 = ReturnType<<T>() => T> // unknown
declare function func(): { a: number; b: string };
type RT4 = ReturnType<typeof func>; // { a: number; b: string }
type RRTE = _ReturnType<string>; // ERROR, Type 'string' does not satisfy the constraint '(...args: any) => any'
type RRTE2 = _ReturnType<Function>;
// ERROR, Type 'Function' does not satisfy the constraint '(...args: any) => any',
// ERROR, Type 'Function' provides no match for the signature '(...args: any): any'.
type RRT = _ReturnType<() => string> // string
type RRT2 = _ReturnType<() => void> // void
type RRT3 = _ReturnType<<T>() => T> // unknown
declare function func(): { a: number; b: string };
type RRT4 = _ReturnType<typeof func>; // { a: number; b: string }