ts相关笔记(Partial、Required、Readonly、Record、Exclude、Extract)

总结一下ts内置的一些常用的工具类型。
TypeScript 内置了一批简单的工具类型,它们就是类型别名的使用方式,同时在全局可用,无需导入。

Partial

它接收一个对象类型,并将这个对象类型的所有属性都标记为可选

实现:

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

使用案例:

type User = {
   
  name: string;
  age: number;
  email: string;
};

type PartialUser = Partial<User>;

const user: User = {
   
  name: 'John Doe',
  age: 30,
  email: 'john.doe@example.com'
};

// 可以不实现全部的属性了!
const partialUser: PartialUser = {
   
  name: 'John Doe',
  age: 30
};

Required

把属性标记为必须

实现:

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

使用案例:

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];
};

使用案例:

interface person {
   
    name: string,
    job: string,
    age: number
}
type readonly = Readonly<person>
// 相当于
// type readonly = {
   
//     readonly name: string;
//     readonly job: string;
//     readonly age: number;
// }

Record

实现:

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

使用案例:

type UserProps = 'name' | 'job' | 'email';

// 等价于你一个个实现这些属性了
type User = Record<UserProps, string>;

const user: User = {
   
  name: 'John Doe',
  job: 'fe-developer',
  email: 'john.doe@example.com'
};

可以使用 Record 类型来声明属性名还未确定的接口类型,如:

type User = Record<string, string>;

const user: User = {
   
  name: 'John Doe',
  job: 'fe-developer',
  email: 'john.doe@example.com',
  bio: 'Make more interesting things!',
  type: 'vip',
  // ...
};

Extract

交集,两个集合的相交部分,即同时存在于这两个集合内的元素组成的集合(两个里面都有的)。

实现:

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

使用案例:

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

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

Exclude

差集,对于 A、B 两个集合来说,A 相对于 B 的差集即为 A 中独有而 B 中不存在的元素 的组成的集合,或者说 A 中剔除了 B 中也存在的元素以后,还剩下的部分(两者里面都没有的)。

实现:

如果 T 是 U 的子类型则返回 never 不是则返回 T

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

使用案例:

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

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

相关推荐

  1. ts快速上手笔记01

    2023-12-29 23:38:01       33 阅读
  2. Golang 切片相关笔记

    2023-12-29 23:38:01       50 阅读
  3. 容器相关笔记

    2023-12-29 23:38:01       48 阅读
  4. UIScrollView的相关笔记

    2023-12-29 23:38:01       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-29 23:38:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-29 23:38:01       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-29 23:38:01       82 阅读
  4. Python语言-面向对象

    2023-12-29 23:38:01       91 阅读

热门阅读

  1. 小程序分享携带参数,被覆盖问题

    2023-12-29 23:38:01       54 阅读
  2. C# 抽象类与接口

    2023-12-29 23:38:01       63 阅读
  3. Leetcode 495. 提莫攻击

    2023-12-29 23:38:01       54 阅读
  4. Red Hat系列Docker安装与移除

    2023-12-29 23:38:01       51 阅读
  5. uniapp-H5项目的坑

    2023-12-29 23:38:01       54 阅读
  6. Python爬虫实战演练之爬去VIP电影

    2023-12-29 23:38:01       57 阅读
  7. 大模型系列课程学习

    2023-12-29 23:38:01       50 阅读