functionfunc1(x: number, y: number): number { return x * y } // func1 的类型为 (x: number, y: number) => number constfunc2: (x: number, y: number) =>number = (x, y) => x + y const func3 = (x: number, y: number): number => x + y
建议:官方推荐使用 interface,当 interface 无法满足,例如需要定义联合类型等,再选择使用 type
联合类型
表示一组可用的类型集合,只要属于其中之一就属于这个联合类型
1
constunion: string | number = 'union'// 表示union可以使string类型也可以是number类型
交叉类型
表示一组类型的叠加,需要满足所有条件才可以属于这个交叉类型,一般用于接口的合并
1 2 3 4 5 6 7 8 9 10 11 12
interface A { name: string } interface B { age: number } constx: A & B = { name: 'shy', age: 18 } // 如果两个类型出现重复的key则每一个key都会进行交叉 // never typeAA = number typeBB = string type U = A & B // U 的类型就会是nuver
/** * Make all properties in T required */ typeRequired<T> = { [P in keyof T]-?: T[P]; }; // ---------- type C = { name?: string | undefined; age?: number | undefined; address?: string | undefined; c?: any; }
type D = Required<C> // type D = { // name: string; // age: number; // address: string; // c: any; // }
/** * Construct a type with a set of properties K of type T */ typeRecord<K extends keyof any, T> = { [P in K]: T; }; // -------- typeAA = Record<string, string> // type AA = { // [x: string]: string; // }
Exclude 移除属于指定类型的部分
1 2 3 4 5 6 7
/** * Exclude from T those types that are assignable to U */ typeExclude<T, U> = T extends U ? never : T; // ---------- type A = Exclude<"name" | "age", "name"> // type A = "age"
Extract 保留指定类型的部分
1 2 3 4 5 6 7 8 9
/** * Extract from T those types that are assignable to U */ typeExtract<T, U> = T extends U ? T : never; // ---------- type A = Extract<"name" | "age", "name"> // type A = "name" type B = Extract<string | number | boolean, number> // type B = number
NonNullable 去除类型中的 null 和 undefined
1 2 3 4 5 6 7
/** * Exclude null and undefined from T */ typeNonNullable<T> = T & {}; // --------- type A = NonNullable<"a" | number | null | undefined> // type A = number | "a"
Pick 以选中的属性生成新的类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/** * From T, pick a set of properties whose keys are in the union K */ typePick<T, K extends keyof T> = { [P in K]: T[P]; }; // --------- typeOBJ = { name: string; age: number; address?: string; c?: any; } type A = Pick<OBJ, "name" | "address"> // type A = { // name: string; // address?: string | undefined; // }
Omit 排除选中的属性,以剩余的属性生成新的类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/** * Construct a type with the properties of T except for those in type K. */ typeOmit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>; // ---------- typeOBJ = { name: string; age: number; address?: string; c?: any; } type A = Omit<OBJ, "name" | "address"> // type A = { // age: number; // c?: any; // }
Parameters 获得函数参数的类型
1 2 3 4 5 6 7 8
/** * Obtain the parameters of a function type in a tuple */ typeParameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never; // --------------- typeFUNC = (x: number, y: number) =>number typeFUNCParam = Parameters<FUNC> // type FUNCParam = [x: number, y: number]
ReturnType 获取函数返回值的类型
1 2 3 4 5 6 7 8
/** * Obtain the return type of a function type */ typeReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any; // --------------- typeFUNC = (x: number, y: number) =>number typeFUNCReturnType = ReturnType<FUNC> // type FUNCReturnType = number