Partial
描述:Partial
将类型 T
的所有属性标记为可选属性
type Partial<T> = {
[P in keyof T]?: T[P];
};
示例
interface Person {
name: string
age: number
}
type PersonPartial = Partial<Person>
// ✅正确
const p1: PersonPartial = {
name: 'Tom',
age: 23,
}
// ✅正确
const p2: PersonPartial = {
name: 'Tom',
}
// ✅正确
const p3: PersonPartial = {
age: 23,
}
// ✅正确
const p4: PersonPartial = {}
Required
描述: Required
将类型 T
的所有属性标记为必选属性
type Required<T> = {
[P in keyof T]-?: T[P];
};
示例
interface Student {
name?: string
age?: number
}
type StudentRequired = Required<Student>
// ✅正确
const t1: StudentRequired = {
name: 'Tom',
age: 23,
}
// ❌错误
// Property 'age' is missing in type ...
const t2: StudentRequired = {
name: 'Tom',
}
// ❌错误
// Property 'name' is missing in type ...
const t3: StudentRequired = {
age: 23,
}
Readonly
描述:Readonly
将类型 T
的所有属性标记为 readonly
, 即不能修改。
type Readonly<T> = {
readonly [P in keyof T]: T[P];
};
示例
interface Person {
name: string
age: number
}
type PersonReadonly = Readonly<Person>
const p1: PersonReadonly = {
name: 'Tom',
age: 23,
}
// ❌错误
// Cannot assign to 'name' because it is a read-only property.
p1.name = 'Anne'
// ❌错误
// Cannot assign to 'age' because it is a read-only property.
p1.age = 25
Pick
描述:Pick
从类型 T
中过滤出某个或某些属性
type Pick<T, K extends keyof T> = {
[P in K]: T[P];
};
示例
interface Person {
name: string
age: number
gender?: 'male' | 'female'
address?: string
}
// 把 'name' 和 'address' 属性从 'Person' 中过滤出来
type PersonPick = Pick<Person, 'name' | 'address'>
// ✅正确
const p1: PersonPick = {
name: 'Tom',
address: 'ShangHai',
}
// ✅正确
const p2: PersonPick = {
name: 'Tom',
}
// ❌错误
// 'age' does not exist in type 'PersonPick'.
const p3: PersonPick = {
name: 'Tom',
age: 23,
}
Record
描述:Record
标记对象的属性(key
)或者属性值(value
)的类型
type Record<K extends keyof any, T> = {
[P in K]: T;
};
示例
interface Person {
name: string
age: number
gender?: 'male' | 'female'
address?: string
}
type PersonRecord = Record<string, Person>
// ✅正确
const r1: PersonRecord = {
'p': {
name: 'Tom',
age: 23,
}
}
// ❌错误
// Type 'string' is not assignable to type 'Person'.
const r2: PersonRecord = {
'p': 'Person'
}
// ❌错误
// Property 'age' is missing in type ...
const r3: PersonRecord = {
'p': {
name: 'Tom',
}
}
Exclude
描述:Exclude
从 T
中排除可分配给 U
的类型
type Exclude<T, U> = T extends U ? never : T;
解析:假如 type T = A | B | C
,那么会被解析为 (A extends U ? never : A) | (B extends U ? never : B) | (C extends U ? never : C)
示例
// 结果:'b' | 'd'
type StringExclude = Exclude<'a' | 'b' | 'c' | 'd', 'a' | 'c' | 'f'>
// ✅正确
const s1: StringExclude = 'b'
// ✅正确
const s2: StringExclude = 'd'
// ❌错误
// Type '"f"' is not assignable to type 'StringExclude'.
const s3: StringExclude = 'f'
Extract
描述:Extract
从 T
中提取可分配给 U
的类型。Exclude
的反操作。
type Extract<T, U> = T extends U ? T : never;
解析:假如 type T = A | B | C
,那么会被解析为 (A extends U ? A : never) | (B extends U ?B : never) | (C extends U ? C : never)
示例
// 结果:'a' | 'c'
type StringExtract = Extract<'a' | 'b' | 'c' | 'd', 'a' | 'c' | 'f'>
// ✅正确
const s1: StringExtract = 'a'
// ✅正确
const s2: StringExtract = 'c'
// ❌错误
// Type '"f"' is not assignable to type 'StringExtract'.
const s3: StringExtract = 'f'