Skip to content

Latest commit

 

History

History
272 lines (197 loc) · 4.31 KB

内置工具.md

File metadata and controls

272 lines (197 loc) · 4.31 KB

Partial将类型的属性都变为可选

interface Info {
    name : string,
    age : number,
}

type optionalInfo = Partial<Info>

得到的新类型为:

type optionalInfo = {
    name?: string | undefined;
    age?: number | undefined;
}

实现Partial:使用映射进行属性遍历

type newPartial<T> = {
    [key in keyof T]?: T[key]
}

Rquired将属性的选项都变为必选

interface Info {
    name? : string,
    age? : number,
}

type optionalInfo = Required<Info>

得到新的类型:

type optionalInfo = {
    name: string;
    age: number;
}

自己实现 Required,也是使用映射进行属性遍历

type newRequired<T> = {
    [key in keyof T]-?: T[key]
}

Readonly将类型所有属性都变为readonly只读

interface Info {
    name? : string,
    age? : number,
}

type optionalInfo = Readonly<Info>

得到新的类型:

type optionalInfo = {
    readonly name?: string | undefined;
    readonly age?: number | undefined;
}

自己实现 Readonly:

type newReadonly<T> = {
    readonly [key in keyof T]: T[key]
}

Record<Keys, Types>构造对象类型,规定属性和值的类型

interface Info {
    name? : string,
    age? : number,
}

type k = "a" | "b" | "c"

type optionalInfo = Record<k, Info>

得到的新类型为:

type optionalInfo = {
    a: Info;
    b: Info;
    c: Info;
}

自己实现 Record

type newRecord<K extends keyof Info, V> = {
    [key in K]: V
}

Pick<Type, Types>构造一个类型,并从给定的类型中选出需要的属性

interface Info {
    name? : string,
    age? : number,
}
type optionalInfo = Pick<Info, "name">

得到新的类型为

type optionalInfo = {
    name?: string | undefined;
}

自己实现 Pick

type newPick<T, K extends keyof T> = {
    [p in K]: T[p]
}

Omit<Type, Keys>构造类型,将给定类型中的指定属性进行过滤

interface Info {
    name? : string,
    age? : number,
}
type optionalInfo = Omit<Info, "name">

得到的新类型为:

type optionalInfo = {
    age?: number | undefined;
}

自己实现 Omit

type newOmit<T, K extends keyof T> = {
    [p in keyof T as p extends K ? never : p]: T[p]
}

Exclude<UnionType, ExcludedMembers>将联合类型中的指定类型排除

type k = "a" | "b" | "c"
type optionalInfo = Exclude<k, "a">

得到的新类型为:

type optionalInfo = "b" | "c"

自己实现 Exclude

type newExclude<T, E> = T extends E ? never : T

Extract<UnionType, ExtractMembers>将联合类型中的指定类型保留

type k = "a" | "b" | "c"
type optionalInfo = Extract<k, "a">

得到的新类型为:

type optionalInfo = "a"

自己实现 Extract:

type newExtract<T, E> = T extends E ? T : never

NoneNullable<Type>构造类型,从中排除掉null,undefined类型

type k = "a" | "b" | "c" | null | undefined
type optionalInfo = NonNullable<k>

得到的新类型为:

type optionalInfo = "a" | "b" | "c"

自己实现 NoneNullable:

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

ReturnType<Type>构造一个含有Type函数返回值的类型

下面会取到函数的返回值类型:

function fn():string{
    return 'a'
}

type callbackType = ReturnType<typeof fn>

得到的新类型为:

type callbackType = string

自己实现 ReturnType:

type newReturnType<T extends (...args:any[])=>any> = T extends (...args:any[])=> infer R ? R : never

InstanceType<Type> 取到构造函数的类型

class Animal {}

function factory<T extends new (...args: any[]) => any>(constructor:T):InstanceType<T>{
    return new constructor()
}

const d1 = factory(Animal)

这样就能保证d1类型的正确

const d1: Animal

自己实现 InstanceType:

type newInstanceType<T extends new (...args:any[])=>any> = T extends new (...args:any[])=> infer R ? R : never