ts常用内置工具类型

本文介绍TypeScript中的Partial、Readonly、Record、Pick、Omit、Exclude、Extract、NonNullable、ReturnType及InstanceType等实用类型技巧,帮助开发者更好地理解和使用这些类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Partial

源码:

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

作用:

Partial 可以快速把某个接口类型中定义的属性变成可选的 (Optional)

Readonly

 源码:

type Foo = {
    readonly bar: number;
    bas: number;
}

const foo: Foo = {bar: 123, bas: 123};
foo.bar = 1 //不能将1分配到foo.bar,因为foo.bar属性是只读的。
foo.bas = 1

interface Foo{
    readonly bar: number;
    bas: number;
}

const foo: Foo = {bar: 123, bas: 123};
foo.bar = 1 //不能将1分配到foo.bar,因为foo.bar属性是只读的。
foo.bas = 1

作用:

readonly用于标记一个属性是只读的,也就是不可修改的。

Record

 源码:

interface PageInfo {
  title: string;
}

type Page = "home" | "about" | "contact";

const nav: Record<Page, PageInfo> = {
  about: { title: "about" },
  contact: { title: "contact" },
  home: { title: "home" },
};

作用:

Record用于定义一个对象的 key 和 value 类型

 

Pick

 源码:

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

interface Person {
  name: string;
  age: number;
  id: number;
  sex: 0 | 1;
}

// 问女生年纪不太礼貌,所以我们不需要 age 这个属性
type Woman = Pick<Person, "name" | "id">;

// 此时 Woman 等效于 Female

interface Female {
  name: string;
  id: number;
}

作用:

采集,Record用于定义一个对象的 key 和 value 类型

 

Omit

 源码:

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

interface User {
  id: number;
  name: string;
  age: number;
  sex: 0 | 1;
  tel: number;
}

type EditUser = Omit<User, "id">; // 就是在 User 的基础上,去掉 id 属性


作用:

省略/剔除,Omit 与 Pick 作用相似,只不过 Omit 是:以一个类型为基础支持剔除某些属性,然后返回一个新类型。

 

Exclude

 源码:

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

interface Person {
    name: string;
}

interface NewPerson {
    name: string;
    age: number;
    id: number;
}

// 案例1
// Person如果extends继承NewPerson(继承了NewPerson的属性),就never异常,否则就返回Person
const obj: Exclude<Person, NewPerson> = {
    name: ''
}



作用:

排除/不包括

Extract

 源码:

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

interface Person {
    name: string;
}

interface NewPerson {
    name: string;
    age: number;
    id: number;
}

// 案例1
//  NewPerson如果extends继承Person(继承了Person的属性),就返回NewPerson,否则就never异常
const obj: Extract<NewPerson, Person> = {
    name: '',
    age: 1,
    id: 1
}




作用:

提取/包括

NonNullable

 源码:

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

作用:

NonNullable工具类型可以从类型 T 中剔除 null 和 undefined 类型,并构建一个新的类型

ReturnType

 源码:

type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;


  type T0 = ReturnType<() => string>; // string
  type T1 = ReturnType<() => { a: string, b: number }>; // { a: string; b: number }

作用:

ReturnType工具类型能获取函数类型 T 的返回值类型

 

InstanceType

 源码:

type InstanceType<T extends abstract new (...args: any) => any> = T extends abstract new (...args: any) => infer R ? R : any;


    class A {
    x = 0;
  }

  type T0 = InstanceType<typeof A>; // A
  type T1 = InstanceType<new (s?: string) => object>; // object

作用:

InstanceType能获取构造函数的返回值类型

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值