TS 常用高级类型

145 阅读3分钟

TypeScript 默认了一些类型程序类型工具,供开发者方便的进行此类操作。下面介绍一些常用的提升效率的类型定义。

// Example interface
interface Example {
    name: string
    age: number
    sex: string
    address: string
    isVip: boolean
    avatar?: string
    company?: string
    selfIntrduction: () => void
}

Pick<T, K>

从类型定义的属性中,选取指定一组属性,返回一个新的类型定义。

type PickExampleA = Pick<Example, 'name' | 'age' | 'sex'>
// 等同于
type PickExampleA = {
    name: string
    age: number
    sex: string
}

Partial<T>

将类型定义的所有属性都定义为可选。

type PartialExampleA = Partial<Example> 
// 等同于
type PartialExampleA = {
    name?: string
    age?: number
    sex?: string
    address?: string
    isVip?: boolean
    avatar?: string
    company?: string
    selfIntrduction?: () => void
}

Readonly<T>

将所有属性定义为可读。

type ReadonlyExampleA = Readonly<Example>
// 等同于
type ReadonlyExampleA = {
    readonly namestring
    readonly age: number
    readonly sexstring
    readonly addressstring
    readonly isVipboolean
    readonly avatar?: string
    readonly company?: string
    readonly selfIntrduction() => void
}

Record<K,T>

以 typeof 格式快速创建一个类型,此类型包含一组指定的属性且都是必填。

type RecordExampleA = Record<'name' | 'sex' | 'address', string>
type RecordExampleB = Record<'zhangsan' | 'lisi', { age: number }>
type RecordExampleC = Record<string, unknown>
// 等同于
type RecordExampleA = {
    name: string
    sex: string
    address: string
}
type RecordExampleB = {
    zhangsan: { age: number }
    lisi: { age: number }
}
type RecordExampleC = {
    [key: string]: unknown
}

Required<T>

与 Partial 相反,将所有类型属性都变成必填。

type RequiredExampleA = Required<Example>
// 等同于
type RequiredExampleA = {
    name: string
    age: number
    sex: string
    address: string
    isVip: boolean
    avatar: string
    company: string
    selfIntrduction: () => void
}

Omit<T, K>

排除接口中指定的属性。

type OmitExampleA = Omit<Example, 'name' | 'age' | 'address' | 'sex'>
// 等同于
type OmitExampleA = {
    isVip: boolean
    avatar?: string
    company?: string
    selfIntrduction: () => void
}

Exclude<T, U>

排除一个联合类型中指定的子类型。

type ExampleType = 'a' | 'b' | 'c' | 'd' | 'e'
type ExcludeExampleA = <ExampleType, 'a' | 'b' | 'c'>
// 等同于
type ExcludeExampleA = 'd' | 'e'

Extract<T, U>

与 Exclude 完全相反,用于提取指定的联合类型。如果不存在提取类型,则返回 never。

type ExtractExampleA = <ExampleType, 'a' | 'b' | 'c'>
type ExtractExampleB = <ExampleType, 'f'>
// 等同于
type ExtractExampleA = 'a' | 'b' | 'c'
type ExtractExampleB = never

NonNullable<T>

过滤掉联合类型中的 null 和 undefined 类型。

type NonNullableExampleANonNullable<string | null | undefined | number>
// 等同于
type NonNullableExampleAstring | number

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

接收函数声明,返回函数的返回值类型,如果多个类型则以联合类型方式返回。

type ReturnTypeExampleA = ReturnType<() => Date>
type ReturnTypeExampleB = ReturnType<(s: string) => string>
// 定义一个函数
function funExample(name: string, age: number):void {}
type ReturnTypeExampleC = ReturnType<typeof funExample>
// 等同于
type ReturnTypeExampleA = Date
type ReturnTypeExampleB = string
type ReturnTypeExampleC = void

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

获取函数的参数类型,将每个参数类型放进一个元祖中。

type ParametersExampleA = Parameters<(name: string, age: number) => string>
// 定义一个函数
function funExample(sex: boolean, avatar?: string):void {}
type ParametersExampleB = Parameters<typeof funExample>
// 等同于
type ParametersExampleA = [name: string, age: string]
type ParametersExampleB = [sex: boolean, avatar?: string | undefined]

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

与 Parameters 类型类似,只是这里获取的是构造函数的全部参数。

interface IEntity {
    count?: () => number
}
interface IEntityConstructor {
    new (a: boolean, b: string): IEntity;
}

type ConstructorParametersExample = ConstructorParameters<IEntityConstructor>
// 等同于
type ConstructorParametersExample = [a: boolean, b: string]

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

获取构造函数的返回类型,如果是多个就以联合类型的方式返回。

type InstanceTypeExample = InstanceType<IEntityConstructor>
// 等同于
type InstanceTypeExample = IEntity

ThisParameterType<T>

获取函数中 this 的数据类型,如果没有则返回 unknown 类型。

interface Foo {
  x: number
}
function fn(this: Foo) {}

type ThisParameterTypeExample = ThisParameterType<typeof fn>
// 等同于
type ThisParameterTypeExample = Foo

OmitThisParameter<T>

移除函数中 this 类型。

interface Foo { x: number }
type Fn = (this: Foo) => void

type OmitThisParameterExample = OmitThisParameter<Fn>
// 等同于
type OmitThisParameterExample = () => void
// 声明此类的函数类型效果如下:
function f(this: void) {} // 此声明在函数内不可使用 this