你不知道的 TypeScript 高级类型

开发 前端
如果只是掌握了 TypeScript 的一些基础类型,可能很难游刃有余的去使用 TypeScript,而且最近 TypeScript 发布了 4.0 的版本新增了更多功能,想要用好它只能不断的学习和掌握它。

[[342075]]

 前言

对于有 JavaScript 基础的同学来说,入门 TypeScript 其实很容易,只需要简单掌握其基础的类型系统就可以逐步将 JS 应用过渡到 TS 应用。

  1. // js 
  2. const double = (num) => 2 * num 
  3.  
  4. // ts 
  5. const double = (num: number): number => 2 * num 

然而,当应用越来越复杂,我们很容易把一些变量设置为 any 类型,TypeScript 写着写着也就成了 AnyScript。为了让大家能更加深入的了解 TypeScript 的类型系统,本文将重点介绍其高级类型,帮助大家摆脱 AnyScript。

泛型

在讲解高级类型之前,我们需要先简单理解泛型是什么。

泛型是强类型语言中比较重要的一个概念,合理的使用泛型可以提升代码的可复用性,让系统更加灵活。下面是维基百科对泛型的描述:

泛型允许程序员在强类型程序设计语言中编写代码时使用一些以后才指定的类型,在实例化时作为参数指明这些类型。

泛型通过一对尖括号来表示(<>),尖括号内的字符被称为类型变量,这个变量用来表示类型。

  1. function copy<T>(arg: T): T { 
  2.   if (typeof arg === 'object') { 
  3.     return JSON.parse( 
  4.       JSON.stringify(arg) 
  5.     ) 
  6.   } else { 
  7.     return arg 
  8.   } 

这个类型 T,在没有调用 copy 函数的时候并不确定,只有调用 copy 的时候,我们才知道 T 具体代表什么类型。

  1. const str = copy<string>('my name is typescript'

类型

我们在 VS Code 中可以看到 copy 函数的参数以及返回值已经有了类型,也就是说我们调用 copy 函数的时候,给类型变量 T 赋值了 string。其实,我们在调用 copy 的时候可以省略尖括号,通过 TS 的类型推导是可以确定 T 为 string 的。

类型推导

高级类型

除了 string、number、boolean 这种基础类型外,我们还应该了解一些类型声明中的一些高级用法。

交叉类型(&)

交叉类型说简单点就是将多个类型合并成一个类型,个人感觉叫做「合并类型」更合理一点,其语法规则和逻辑 “与” 的符号一致。

  1. T & U 

假如,我现在有两个类,一个按钮,一个超链接,现在我需要一个带有超链接的按钮,就可以使用交叉类型来实现。

  1. interface Button { 
  2.   type: string 
  3.   text: string 
  4.  
  5. interface Link { 
  6.   alt: string 
  7.   href: string 
  8.  
  9. const linkBtn: Button & Link = { 
  10.   type: 'danger'
  11.   text: '跳转到百度'
  12.   alt: '跳转到百度'
  13.   href: 'http://www.baidu.com' 

联合类型(|)

联合类型的语法规则和逻辑 “或” 的符号一致,表示其类型为连接的多个类型中的任意一个。

  1. T | U 

例如,之前的 Button 组件,我们的 type 属性只能指定固定的几种字符串。

  1. interface Button { 
  2.   type: 'default' | 'primary' | 'danger' 
  3.   text: string 
  4.  
  5. const btn: Button = { 
  6.   type: 'primary'
  7.   text: '按钮' 

类型别名(type)

前面提到的交叉类型与联合类型如果有多个地方需要使用,就需要通过类型别名的方式,给这两种类型声明一个别名。类型别名与声明变量的语法类似,只需要把 const、let 换成 type 关键字即可。

  1. type Alias = T | U 
  2. type InnerType = 'default' | 'primary' | 'danger' 
  3.  
  4. interface Button { 
  5.   type: InnerType 
  6.   text: string 
  7.  
  8. interface Alert { 
  9.   type: ButtonType 
  10.   text: string 

类型索引(keyof)

keyof 类似于 Object.keys ,用于获取一个接口中 Key 的联合类型。

  1. interface Button { 
  2.     type: string 
  3.     text: string 
  4.  
  5. type ButtonKeys = keyof Button 
  6. // 等效于 
  7. type ButtonKeys = "type" | "text" 

还是拿之前的 Button 类来举例,Button 的 type 类型来自于另一个类 ButtonTypes,按照之前的写法,每次 ButtonTypes 更新都需要修改 Button 类,如果我们使用 keyof 就不会有这个烦恼。

  1. interface ButtonStyle { 
  2.     color: string 
  3.     background: string 
  4. interface ButtonTypes { 
  5.     default: ButtonStyle 
  6.     primary: ButtonStyle 
  7.     danger: ButtonStyle 
  8. interface Button { 
  9.     type: 'default' | 'primary' | 'danger' 
  10.     text: string 
  11.  
  12. // 使用 keyof 后,ButtonTypes修改后,type 类型会自动修改  
  13. interface Button { 
  14.     type: keyof ButtonTypes 
  15.     text: string 

类型约束(extends)

这里的 extends 关键词不同于在 class 后使用 extends 的继承作用,泛型内使用的主要作用是对泛型加以约束。我们用我们前面写过的 copy 方法再举个例子:

  1. type BaseType = string | number | boolean 
  2.  
  3. // 这里表示 copy 的参数 
  4. // 只能是字符串、数字、布尔这几种基础类型 
  5. function copy<T extends BaseType>(arg: T): T { 
  6.   return arg 

copy number

如果我们传入一个对象就会有问题。

copy object

 

extends 经常与 keyof 一起使用,例如我们有一个方法专门用来获取对象的值,但是这个对象并不确定,我们就可以使用 extends 和 keyof 进行约束。

  1. function getValue<T, K extends keyof T>(obj: T, key: K) { 
  2.   return obj[key
  3.  
  4. const obj = { a: 1 } 
  5. const a = getValue(obj, 'a'

获取对象的值

这里的 getValue 方法就能根据传入的参数 obj 来约束 key 的值。

类型映射(in)

in 关键词的作用主要是做类型的映射,遍历已有接口的 key 或者是遍历联合类型。下面使用内置的泛型接口 Readonly 来举例。

  1. type Readonly<T> = { 
  2.     readonly [P in keyof T]: T[P]; 
  3. }; 
  4.  
  5. interface Obj { 
  6.   a: string 
  7.   b: string 
  8.  
  9. type ReadOnlyObj = Readonly<Obj> 

ReadOnlyObj

我们可以结构下这个逻辑,首先 keyof Obj 得到一个联合类型 'a' | 'b'。

  1. interface Obj { 
  2.     a: string 
  3.     b: string 
  4.  
  5. type ObjKeys = 'a' | 'b' 
  6.  
  7. type ReadOnlyObj = { 
  8.     readonly [P in ObjKeys]: Obj[P]; 

然后 P in ObjKeys 相当于执行了一次 forEach 的逻辑,遍历 'a' | 'b'

  1. type ReadOnlyObj = { 
  2.     readonly a: Obj['a']; 
  3.     readonly b: Obj['b']; 

最后就可以得到一个新的接口。

  1. interface ReadOnlyObj { 
  2.     readonly a: string; 
  3.     readonly b: string; 

条件类型(U ? X : Y)

条件类型的语法规则和三元表达式一致,经常用于一些类型不确定的情况。

  1. T extends U ? X : Y 

上面的意思就是,如果 T 是 U 的子集,就是类型 X,否则为类型 Y。下面使用内置的泛型接口 Extract 来举例。

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

如果 T 中的类型在 U 存在,则返回,否则抛弃。假设我们两个类,有三个公共的属性,可以通过 Extract 提取这三个公共属性。

  1. interface Worker { 
  2.   name: string 
  3.   age: number 
  4.   email: string 
  5.   salary: number 
  6.  
  7. interface Student { 
  8.   name: string 
  9.   age: number 
  10.   email: string 
  11.   grade: number 
  12.  
  13.  
  14. type CommonKeys = Extract<keyof Worker, keyof Student> 
  15. // 'name' | 'age' | 'email' 

CommonKeys

工具泛型

TypesScript 中内置了很多工具泛型,前面介绍过 Readonly、Extract 这两种,内置的泛型在 TypeScript 内置的 lib.es5.d.ts 中都有定义,所以不需要任何依赖都是可以直接使用的。下面看看一些经常使用的工具泛型吧。

lib.es5.d.ts

Partial

  1. type Partial<T> = { 
  2.     [P in keyof T]?: T[P] 

Partial 用于将一个接口的所有属性设置为可选状态,首先通过 keyof T,取出类型变量 T 的所有属性,然后通过 in 进行遍历,最后在属性后加上一个 ?。

我们通过 TypeScript 写 React 的组件的时候,如果组件的属性都有默认值的存在,我们就可以通过 Partial 将属性值都变成可选值。

  1. import React from 'react' 
  2.  
  3. interface ButtonProps { 
  4.   type: 'button' | 'submit' | 'reset' 
  5.   text: string 
  6.   disabled: boolean 
  7.   onClick: () => void 
  8.  
  9. // 将按钮组件的 props 的属性都改为可选 
  10. const render = (props: Partial<ButtonProps> = {}) => { 
  11.   const baseProps = { 
  12.     disabled: false
  13.     type: 'button'
  14.     text: 'Hello World'
  15.     onClick: () => {}, 
  16.   } 
  17.   const options = { ...baseProps, ...props } 
  18.   return ( 
  19.     <button 
  20.       type={options.type} 
  21.       disabled={options.disabled} 
  22.       onClick={options.onClick}> 
  23.       {options.text} 
  24.     </button> 
  25.   ) 

Required

  1. type Required<T> = { 
  2.     [P in keyof T]-?: T[P] 

Required 的作用刚好与 Partial 相反,就是将接口中所有可选的属性改为必须的,区别就是把 Partial 里面的 ? 替换成了 -?。

Record

  1. type Record<K extends keyof any, T> = { 
  2.     [P in K]: T 

Record 接受两个类型变量,Record 生成的类型具有类型 K 中存在的属性,值为类型 T。这里有一个比较疑惑的点就是给类型 K 加一个类型约束,extends keyof any,我们可以先看看 keyof any 是个什么东西。

keyof any

大致一直就是类型 K 被约束在 string | number | symbol 中,刚好就是对象的索引的类型,也就是类型 K 只能指定为这几种类型。

我们在业务代码中经常会构造某个对象的数组,但是数组不方便索引,所以我们有时候会把对象的某个字段拿出来作为索引,然后构造一个新的对象。假设有个商品列表的数组,要在商品列表中找到商品名为 「每日坚果」的商品,我们一般通过遍历数组的方式来查找,比较繁琐,为了方便,我们就会把这个数组改写成对象。

  1. interface Goods { 
  2.   id: string 
  3.  name: string 
  4.   price: string 
  5.   image: string 
  6.  
  7. const goodsMap: Record<string, Goods> = {} 
  8. const goodsList: Goods[] = await fetch('server.com/goods/list'
  9.  
  10. goodsList.forEach(goods => { 
  11.   goodsMap[goods.name] = goods 
  12. }) 

Pick

  1. type Pick<T, K extends keyof T> = { 
  2.     [P in K]: T[P] 

Pick 主要用于提取接口的某几个属性。做过 Todo 工具的同学都知道,Todo工具只有编辑的时候才会填写描述信息,预览的时候只有标题和完成状态,所以我们可以通过 Pick 工具,提取 Todo 接口的两个属性,生成一个新的类型 TodoPreview。

  1. interface Todo { 
  2.   title: string 
  3.   completed: boolean 
  4.   description: string 
  5.  
  6. type TodoPreview = Pick<Todo, "title" | "completed"
  7.  
  8. const todo: TodoPreview = { 
  9.   title: 'Clean room'
  10.   completed: false 

TodoPreview

Exclude

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

Exclude 的作用与之前介绍过的 Extract 刚好相反,如果 T 中的类型在 U 不存在,则返回,否则抛弃。现在我们那之前的两个类举例,看看 Exclude 的返回结果。

  1. interface Worker { 
  2.   name: string 
  3.   age: number 
  4.   email: string 
  5.   salary: number 
  6.  
  7. interface Student { 
  8.   name: string 
  9.   age: number 
  10.   email: string 
  11.   grade: number 
  12.  
  13.  
  14. type ExcludeKeys = Exclude<keyof Worker, keyof Student> 
  15. // 'name' | 'age' | 'email' 

ExcludeKeys

取出的是 Worker 在 Student 中不存在的 salary。

Omit

  1. type Omit<T, K extends keyof any> = Pick< 
  2.   T, Exclude<keyof T, K> 

Omit 的作用刚好和 Pick 相反,先通过 Exclude 先取出类型 T 中存在,但是 K 不存在的属性,然后再由这些属性构造一个新的类型。还是通过前面的 Todo 案例来说,TodoPreview 类型只需要排除接口的 description 属性即可,写法上比之前 Pick 精简了一些。

  1. interface Todo { 
  2.   title: string 
  3.   completed: boolean 
  4.   description: string 
  5.  
  6. type TodoPreview = Omit<Todo, "description"
  7.  
  8. const todo: TodoPreview = { 
  9.   title: 'Clean room'
  10.   completed: false 

TodoPreview

总结

如果只是掌握了 TypeScript 的一些基础类型,可能很难游刃有余的去使用 TypeScript,而且最近 TypeScript 发布了 4.0 的版本新增了更多功能,想要用好它只能不断的学习和掌握它。希望阅读本文的朋友都能有所收获,摆脱 AnyScript。

本文转载自微信公众号「更了不起的前端」,可以通过以下二维码关注。转载本文请联系更了不起的前端公众号。

 

责任编辑:武晓燕 来源: 更了不起的前端
相关推荐

2021-10-17 13:10:56

函数TypeScript泛型

2020-07-28 08:26:34

WebSocket浏览器

2020-06-12 09:20:33

前端Blob字符串

2020-08-05 12:17:00

C语言代码分配

2022-08-10 09:03:35

TypeScript前端

2010-08-23 09:56:09

Java性能监控

2021-07-05 05:34:10

Typescript语言开发

2015-06-19 13:54:49

2012-11-23 10:57:44

Shell

2020-08-11 11:20:49

Linux命令使用技巧

2021-12-29 11:38:59

JS前端沙箱

2021-12-22 09:08:39

JSON.stringJavaScript字符串

2009-12-10 09:37:43

2011-09-15 17:10:41

2021-02-01 23:23:39

FiddlerCharlesWeb

2022-10-13 11:48:37

Web共享机制操作系统

2010-04-10 13:06:24

Windows Emb

2018-12-06 09:12:58

2017-12-15 13:44:22

2021-11-16 08:51:29

Node JavaScript变量类型
点赞
收藏

51CTO技术栈公众号