将类型的属性变成必选
interface Person {
name?: string
age?: number
hobby?: string[]
}
// 下面的类型不用定义, TypeScript自带
type Required<T> = {
// -? 非常有意思, 相当于把可选的去掉
[key in keyof T]-?: T[Key]
}
// Required 包裹之后 Person的所有属性变为必选了
const user1: Required<Person> = {
name: "Ervin Howell",
age: 26,
hobby: ['code', '...']
}
Partial与Required相反, 将所有属性变为可选
interface Person {
name: string
age: number
hobby: string[]
}
// 下面的类型不用定义, TypeScript自带
type Partial<I> = {
[k in keyof T]?: T[K]
}
// Partial 包裹之后 Person的所有属性变为可选了
const user1: Partial<Person> = {
name: "Ervin Howell",
age: 26,
}
Exclude
的作用是将某个类型中属于另一个的类型移除掉, 剩余部分构成新的类型 去除UnitTypeShort类型中的 ‘D’, ‘M’
// 下面的类型不用定义, TypeScript自带
type Exclude<T, U> = T extends U? never : T
type UnitTypeShort = 'd' | 'D' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms'
const foo: Exclude<UnitTypeShort, 'D' | 'M'> = 'd'
const bar1: Exclude<UnitTypeShort, 'D' | 'M'> = 'D'
// 类型“"D"”不可分配给类型“"d" | "y" | "h" | "m" | "s" | "ms"”。你的意思是“"d"”?ts(2820)
const bar2: Exclude<UnitTypeShort, 'D' | 'M'> = 'M'
// 类型“"M"”不可分配给类型“"d" | "y" | "h" | "m" | "s" | "ms"”。你的意思是“"m"”?ts(2820)
和 Exclude
相反, Extract 从T中提取出U, 适用于并集类型 从T中提取出U
// 下面的类型不用定义, TypeScript自带
type Extract<T, U> = T extends U ? T : never;
const foo: Extract<'a' | 'b' | 'c', 'a' | 'b' | 'd'> = 'd'**加粗样式**
'a' | 'b' | 'c' extends 'a' | 'b' | 'd'? T :never
step1:('a' extends 'a' | 'b' | 'd'?'a':never) => 'a'
step2:('b' extends 'a' | 'b' | 'd'?'b':never) => 'b'
step3: ('c' extends 'a' | 'b' | 'd' ? 'c' : never) => never
res: 'a' | 'b' | never=>'a' | 'b'
泛型+extends 泛型传的是联合类型 先拆解判断再组装成联合类型
数组或对象的所有属性值转换为只读, 这些属性不能被重新赋值
// 下面的类型不用定义, TypeScript自带
type Readonly<T> = {
readonly [P in keyof T]: T[P];
}
interface Person {
name: string
age: number
hobby: string[]
}
const user: Readonly<Person> = {
name: 'Ervin Howell',
age: 26,
hobby: ['code', '...']
}
user.age = 25
// 无法为“age”赋值,因为它是只读属性。ts(2540)
Record
的作用是将K中的所有属性的值转换为T类型
// 下面的类型不用定义, TypeScript自带
type Record<K extends keyof any, T> = {
[P in K]: T;
};
interface Person {
name: string
age: number
hobby: string[]
}
const correct: Record<Pe, string> = {
name: 'Ervin Howell',
age: '26岁'
}
const error: Record<Pe, string> = {
name: 'Ervin Howell',
age: 26
}
// 不能将类型“number”分配给类型“string”。ts(2322)
从某个类型中挑出一些属性出来
// 下面的类型不用定义, TypeScript自带
type Pick<T, K extends keyof T> = {
[P in K]: T[P];
};
interface Person {
name: string
age: number
hobby: string[]
}
const foo: Pick<Person, 'name' | 'age'> = {
name: 'Ervin Howell',
age: 26
}
从某个类型中取出出去K的其他所有属性(与Pick相反)
// 下面的类型不用定义, TypeScript自带
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
interface Person {
name: string
age: number
hobby: string[]
}
// 取出 除去 'age'和'hobby'之外的属性
const foo: Omit<Person, 'age' | 'hobby'> = {
name: 'Ervin Howell'
}
去除类型中的null和undefined
// 下面的类型不用定义, TypeScript自带
type NonNullable<T> = T & {};
type p1 = NonNullable<string | null | undefined>
// type p1 = string
type p2 = NonNullable<string[] | number | undefined>
// type p2 = number | string[]
type p3 = NonNullable<string[] | number[] | { name: string } | undefined>
// type p3 = string[] | { name: string; } | number[]
用来得到一个函数的返回值类型
// 下面的类型不用定义, TypeScript自带
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any
type Fun2 = (value: string) => string
type off = ReturnType<Fun2>
// type off = string
用于获得函数的参数类型所组成的元组类型
// 下面的类型不用定义, TypeScript自带
type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;
type p1 = Parameters<(a: number, b: string) => string>
// type p1 = [a: number, b: string]