一、Partial、Required、Readonly
转化为可选参数/必传参数/只读属性
// 1,Partial转化为可选参数
interface PartialData {
title: string;
description: string;
}
type PartialModel = Partial<PartialData>
// 相当于
// type PartialModel = {
// title?: string | undefined;
// description?: string | undefined;
// }
// 2,Required把可选转为必选
interface Props {
a?: number;
b?: string;
}
const obj: Props = { a: 5 }; // OK
const obj2: Required<Props> = { a: 5 }; // 错误:类型 "{ a: number; }" 中缺少属性 "b",但类型 "Required<Props>" 中需要该属性。
// 3,Readonly转化为只读属性
interface Todo {
title: string;
}
const todo1: Readonly<Todo> = {
title: 'Delete inactive users',
};
todo1.title = 'Hello'; // 错误:无法分配到 "title" ,因为它是只读属性。
const todo2 = Object.freeze({ // Object.freeze可以把对象冻结为只读
title: 'Delete inactive users',
})
todo2.title = 'Hello'; // 无法分配到 "title" ,因为它是只读属性。
二、Record
将某个类型的属性映射到另一个类型上
interface PageInfo {
title: string;
}
type Page = 'home' | 'about' | 'contact';
const x: Record<Page, PageInfo> = { // Record<Page, PageInfo>相当于:type X = {[key in Page]:PageInfo}
about: { title: 'about' },
contact: { title: 'contact' },
home: { title: 'home' },
};
三、Pick、Omit
从接口<第一个参数>中挑选/剔除部分属性<第二个参数>来构造类型。
interface Todo {
title: string;
description: string;
completed: boolean;
}
type TodoPreviewPick = Pick<Todo, 'title' | 'completed'>; // Pick
type TodoPreviewOmit = Omit<Todo, 'description'>; // Omit
const todo1: TodoPreviewPick = {
title: 'Clean room',
completed: false,
};
const todo2: TodoPreviewOmit = {
title: 'Clean room',
completed: false,
};
四、Exclude、Extract
第一个参数必须要是Type类型,并从里面剔除/提取 第二个参数的属性,然后构造一个类型。
// Exclude
type T0 = Exclude<'a' | 'b' | 'c', 'a'>; // "b" | "c"
type T1 = Exclude<'a' | 'b' | 'c', 'a' | 'b'>; // "c"
type T2 = Exclude<string | number | (() => void), Function>; // string | number
// Extract
type T0 = Extract<'a' | 'b' | 'c', 'a' | 'f'>; // "a"
type T1 = Extract<string | number | (() => void), Function>; // () => void
五、NonNullable
从类型 Type 中剔除 null 和 undefined ,然后构造一个类型。
type T0 = NonNullable<string | number | undefined>; // string | number
type T1 = NonNullable<string[] | null | undefined>; // string[]
六、Parameters
把函数的参数作为元组返回一个新的类型
declare function f1(arg: { a: number; b: string }): void;
type T0 = Parameters<() => string>;
// []
type T1 = Parameters<(s: string) => void>;
// [s: string]
type T2 = Parameters<<T>(arg: T) => T>;
// [arg: unknown]
type T3 = Parameters<typeof f1>;
// [arg: { a: number; b: string; }]
type T4 = Parameters<any>;
// unknown[]
type T5 = Parameters<never>;
// never
type T6 = Parameters<string>;
// never
// 类型“string”不满足约束“(...args: any) => any”。
type T7 = Parameters<Function>;
// never
// 类型“string”不满足约束“(...args: any) => any”。
七、ConstructorParameters?不理解有啥用
由构造函数类型 Type 的参数类型来构建出一个元组类型。(若 Type 不是构造函数类型,则返回 never )。
type T0 = ConstructorParameters<ErrorConstructor>;
// [message?: string | undefined]
type T1 = ConstructorParameters<FunctionConstructor>;
// string[]
type T2 = ConstructorParameters<RegExpConstructor>;
// [pattern: string | RegExp, flags?: string | undefined]
type T3 = ConstructorParameters<any>;
// unknown[]
type T4 = ConstructorParameters<Function>;
// never
// 类型“Function”不满足约束“abstract new (...args: any) => any”。
// 类型“Function”提供的内容与签名“new (...args: any): any”不匹配
八、ReturnType
由函数类型的返回值 来构建一个新类型。
declare function f1(arg: { a: number; b: string }): { a: number; b: string };
type T0 = ReturnType<() => string>; // string
type T1 = ReturnType<(s: string) => void>; // void
type T2 = ReturnType<(<T>() => T)>; // unknown
type T3 = ReturnType<(<T extends U, U extends number[]>() => T)>; // number[]
type T4 = ReturnType<typeof f1>; // { a: number, b: string }
type T5 = ReturnType<any>; // any
type T6 = ReturnType<never>; // never
type T7 = ReturnType<string>; // 错误:类型“string”不满足约束“(...args: any) => any”。
type T8 = ReturnType<Function>; // 错误:类型“Function”不满足约束“(...args: any) => any”。类型“Function”提供的内容与签名“(...args: any): any”不匹配。
九、InstanceType
由构造函数类型的实例类型 来构建一个新类型。
type T0 = InstanceType<typeof C>; // C
type T1 = InstanceType<any>; // any
type T2 = InstanceType<never>; // never
type T3 = InstanceType<string>; // 错误:类型“string”不满足约束“abstract new (...args: any) => any”。
type T4 = InstanceType<Function>; // 错误:类型“Function”不满足约束“abstract new (...args: any) => any”。类型“Function”提供的内容与签名“new (...args: any): any”不匹配。
十、ThisParameterType
从函数类型中提取 this 参数的类型。 若函数类型不包含 this 参数,则返回 unknown 类型。
function toHex(this: Number) {
return this.toString(16);
}
function numberToString(n: ThisParameterType<typeof toHex>) {
return toHex.apply(n);
}
const re = numberToString(1); // "1"