typescript实用工具类

目录

一、与对象相关的工具类

1、Partial

2、Required

3、Readonly

4、 Record,type>

5、 Pick,keys>

6、Omit,keys>

二、与类型有关的工具类 

1、Exclude ,excludedunion>

2、Extract,>

3、NonNullable 

三、与函数相关的工具类


一、与对象相关的工具类

1、Partial<type>

(1)含义:将type的所有类型设置为可选

(2)示例:

interface ITest1{
  name:string
  age:number
}
type ITest2 = Partial<ITest1>

ITest2相当于 

interface ITest2{
  name?:string
  age?:number
}

2、Required<type>

(1)含义:构建一个类型,使类型Type的所有属性为required。 与此相反的是Partial

(2)使用:

interface Props {
    a?: number;
    b?: string;
}

const obj: Props = { a: 5 }; // OK

const obj2: Required<Props> = { a: 5 }; // Error: property 'b' missing

3、Readonly<type>

(1)含义:类型里面的属性都无法再被赋值

(2)使用:

interface ITode {
  title: string
}

const a: Readonly<ITode> = {
  title: 'xxxx',
}
a.title = 'jjjj' //报错,title为只读属性

4、 Record<keys,type>

(1)含义:用来将某个类型的属性映射到另一个类型上。

const type1 = {'key':{type2}}

type1类型里面的value值都是type2类型的时候适用。

(2)使用:

interface AValue {
  name?: string
  age?: number
}
type AKey = 'people1' | 'people2'

type A = Record<AKey, AValue>
const human: A = {
  people1: {
    name: '2',
    age: 2,
  },
  people2: {
    name: '3',
  },
}

A类型的值类型都是AValue类型,常规写法相当于:

interface AValue {
  name?: string
  age?: number
}
type A = {
  people1: AValue
  people2: AValue
  people3: AValue
}
const human: A = {
  people1: {
    name: '2',
    age: 2,
  },
  people2: {
    name: '3',
  },
  people3: {
    age: 2,
  },
}
interface AValue {
  name?: string
  age?: number
}

type A = Record<string, AValue>
const human: A = {
  people1: {
    name: '2',
    age: 2,
  },
}

这样写不必非要有people1和people2两个key值

5、 Pick<type,keys>

(1)含义:从类型Type中挑选部分属性Keys来构造类型。key of type,key必须是type里面的。

(2)使用:

interface People1 {
  name: string
  age: number
  children: number
}
type People2 = Pick<People1, 'name' | 'age'>

const a: People2 = {
  name: '1',
  age: 2,
}

6、Omit<type,keys>

(1)含义:从类型Type中获取所有属性,然后从中剔除Keys属性后构造一个类型。key of type,key必须是type里面的。

(2)使用:

interface Todo {
    title: string;
    description: string;
    completed: boolean;
}

type TodoPreview = Omit<Todo, 'description'>;

const todo: TodoPreview = {
    title: 'Clean room',
    completed: false,
};

二、与类型有关的工具类 

1、Exclude<type,excludedUnion> 

(1)含义:从类型Type中剔除所有可以赋值给ExcludedUnion的属性,然后构造一个类型。

(2)使用:

type T0 = Exclude<'a' | 'b' | 'c', 'a'>; // "b" | "c"
type T1 = Exclude<'a' | 'b' | 'c', 'a' | 'b'>; // "c"
type T2 = Exclude<string | number | (() => void), Function>; // string | number

2、Extract<Type, Union>

(1)含义:从类型Type中提取所有可以赋值给Union的类型,然后构造一个类型。

(2)使用:

type T0 = Extract<'a' | 'b' | 'c', 'a' | 'f'>; // "a"
type T1 = Extract<string | number | (() => void), Function>; // () => void

3、NonNullable<type> 

(1)含义:非空的,从类型Type中剔除nullundefined,然后构造一个类型。

(2)使用:

type T0 = NonNullable<string | number | undefined>; // string | number
type T1 = NonNullable<string[] | null | undefined>; // string[]

三、与函数相关的工具类

使用的频率较低,待有空时补充。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值