TS 里几个常用的内置工具类型

更新时间:2023-04-09 16:53:26 阅读:96 所属栏目: 日志

Record

Record的内部定义,接收两个泛型参数;Record后面的泛型就是对象键和值的类型

作用: 定义一个对象的 key 和 value 类型

源码:

Record<key type, value type> 

Record<string, never>空对象

Record<string, unknown>任意对象

{}任何不为空的对象

type Record<K extends string | number | symbol, T> = {
    [P in K]: T;
}

逐步解析

泛型K即为第一次参数

p in xx又是什么意思呢?

in的意思就是遍历,如上就是将 类型string进行遍历,也就是string

每个属性都是传入的T类型,如 string: PersonModel

Partial

源码:

type Partial<T> = {
    [P in keyof T]?: Partial<T[P]>
}

作用:生成一个新类型,该类型与 T 拥有相同的属性,但是所有属性皆为可选项

interface Foo {
    name: string
    age: number
}
type Bar = Partial<Foo>
// 相当于
type Bar = {
    name?: string
    age?: number
}

Required

源码:

type Require<T> = {
    [P in keyof <T>]-?: T[P]
}

作用:生成一个新类型,该类型与 T 拥有相同的属性,但是所有属性皆为必选项

interface Foo {
    name: string
    age?: number
}
type Bar = Required<Foo>
// 相当于
type Bar = {
    name: string
    age: number
}

Readonly

源码:

type Readonly<T> = {
  readonly [P in keyof T]: T[P];
};

作用:生成一个新类型,T 中的 K 属性是只读的,K 属性是不可修改的。

interface Foo {
    name: string
    age: number
}
type Bar = Readonly<Foo>
// 相当于
type Bar = {
    readonly name: string
    readonly age: number
}

Pick

源码:

type Pick<T, K extends keyof T> = {
    [P in K]: T[P];
};

作用:生成一个新类型,映射类型 ; P in K 类似于 js的 for…in语句 ; extends 为泛型约束

interface Foo {
    name: string;
    age?: number;
    gender: string;
}
type Bar = Pick<Foo, 'age' | 'gender'>
// 相当于
type Bar = {
    age?: number
    gender: string
}

const todo: Bar= {
   age?: 3,
   gender: 男
};
console.log(todo)

Exclude

源码:

type Exclude<T, U> = T extends U ? never : T

作用:如果 TU 的子类型则返回 never 不是则返回 T

type A = number | string | boolean
type B = number | boolean

type Foo = Exclude<A, B>
// 相当于
type Foo = string

Extract

type Extract<T, U> = T extends U ? T : never

作用: 和 Exclude 相反

type A = number | string | boolean
type B = number | boolean

type Foo = Extract<A, B>
// 相当于
type Foo = number | boolean

Omit

源码:

type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>

作用:生成一个新类型,该类型拥有 T 中除了 K 属性以外的所有属性; Exclude<keyof T, K> 过滤掉 T 中的 K属性 ;

type Foo = {
	name: string
	age: number
}

type Bar = Omit<Foo, 'age'>
// 相当于
type Bar = {
	name: string
}

NonNullable

源码

NonNullable<T>

作用:从泛型 T 中排除掉 nullundefined

type NonNullable<T> = T extends null | undefined ? never : T;

type t = NonNullable<'name' | undefined | null>;
/* type t = 'name' */

Parameters

源码:

Parameters<T extends (...args: any) => any>

作用:以元组的方式获得函数的入参类型

type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;

type t = Parameters<(name: string) => any>; // type t = [string]

type t2 = Parameters<((name: string) => any)  | ((age: number) => any)>; // type t2 = [string] | [number]

ConstructorParameters

源码:

ConstructorParameters<T extends new (...args: any) => any>

作用:以元组的方式获得构造函数的入参类型

type ConstructorParameters<T extends new (...args: any) => any> = T extends new (...args: infer P) => any ? P : never;
 
type t = ConstructorParameters<(new (name: string) => any)  | (new (age: number) => any)>;
// type t = [string] | [number]

ReturnType

源码:

ReturnType<T extends (...args: any) => any>

作用:获得函数返回值的类型

type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
 
type t = ReturnType<(name: string) => string | number>
// type t = string | number

InstanceType

源码:

InstanceType<T extends new (...args: any) => any>

作用:获得构造函数返回值的类型

type InstanceType<T extends new (...args: any) => any> = T extends new (...args: any) => infer R ? R : any;
 
type t = InstanceType<new (name: string) => {name: string, age: number}>
/* 
type h = {
    name: string;
    age: number;
}
*/