Partial
/*** Make all properties in T optional*/type Partial<T> = {[P in keyof T]?: T[P];};
示例:
interface You {name: string}type Me = Partial<You>=>type Me = {name?: string}
Required
/*** Make all properties in T required*/type Required<T> = {[P in keyof T]-?: T[P];};
示例:
interface You {name?: string}type Me = Required<You>=>type Me = {name: string}
Readonly
/*** Make all properties in T readonly*/type Readonly<T> = {readonly [P in keyof T]: T[P];};
示例:
interface You {name: string}type Me = Readonly<You>=>type Me = {readonly name: string}
Pick
/*** From T, pick a set of properties whose keys are in the union K*/type Pick<T, K extends keyof T> = {[P in K]: T[P];};
示例:
interface You {name: stringage?: numbergender: string}type Me = Pick<You, 'age' | 'gender'>=>type Me = {age?: stringgender: string}
Record
/*** Construct a type with a set of properties K of type T*/type Record<K extends keyof any, T> = {[P in K]: T;};
示例:
interface You {name: stringage?: number}type Me = Record<You,string>=>type Me = {name: stringage?: string}
Exclude
/*** Exclude from T those types that are assignable to U*/type Exclude<T, U> = T extends U ? never : T;
示例:
type A = number | string | booleantype B = number | booleantype Foo = Exclude<A, B>=>type Foo = string
Extract
/*** Extract from T those types that are assignable to U*/type Extract<T, U> = T extends U ? T : never;
示例:
type A = number | string | booleantype B = number | booleantype Foo = Extract<A, B>=>type Foo = number | boolean
Omit
/*** Construct a type with the properties of T except for those in type K.*/type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
示例:
type Foo = {name: stringage: number}type Bar = Omit<Foo, 'age'>=>type Bar = {name: string}
