Exclude 工具类型八个使用技巧

开发 开发工具
本文我将介绍 Exclude​ 工具类型的 8 个使用技巧,掌握这些技巧之后,在工作中你就能更好地利用 Exclude 工具类型来满足不同的使用场景。

Exclude 是 TypeScript 中内置的工具类型,它用于从一个联合类型中排除掉你不希望包含的类型,生成一个新的类型。这个工具类型在日常开发中非常有用,它能够帮助我们编写类型安全的代码和更好地实现代码复用。

/**
 * Exclude from T those types that are assignable to U.
 * typescript/lib/lib.es5.d.ts
 */
type Exclude<T, U> = T extends U ? never : T;

type T0 = Exclude<"a" | "b" | "c", "a" | "b">
// type T0 = "c"

本文我将介绍 Exclude 工具类型的 8 个使用技巧,掌握这些技巧之后,在工作中你就能更好地利用 Exclude 工具类型来满足不同的使用场景。

1.排除指定的基本数据类型

type MyTypes = string | number | boolean;
type StringOrNumber = Exclude<MyTypes, boolean>;

let uid: StringOrNumber = "semlinker" // Ok
uid = 2024 // Ok
uid = false // Error
// Type 'boolean' is not assignable to type 'StringOrNumber'.

2.排除 string 或 number 类型的子类型

type Status = "success" | "error" | 200 | 500;

type StringStatus = Exclude<Status, number>; 
// type StringStatus = "success" | "error"

type NumberStatus = Exclude<Status, string>
// type NumberStatus = 200 | 500

3.排除两个联合类型的共有成员

type TaskStatus = "Todo" | "InProgress" | "Done" | "Archived";
type ModuleHandledStatus = "Todo" | "Done" | "OnHold";

type TaskOnlyStatus = Exclude<TaskStatus, ModuleHandledStatus>;
// type TaskOnlyStatus = "InProgress" | "Archived"

4.排除含有特定属性的子类型

Animal 联合类型,包含了多种动物的描述对象,我们想从中排除含有 "legs" 属性的子类型。

type Animal =
    | { type: 'dog', legs: number }
    | { type: 'cat', legs: number }
    | { type: 'fish', fins: number };
type AnimalsWithFins = Exclude<Animal, { legs: number }>;

const fish: AnimalsWithFins = { type: 'fish', fins: 6 }; // Ok
const dog: AnimalsWithFins = { type: 'dog', legs: 4 }; // Error
// Type '"dog"' is not assignable to type '"fish"'.

5.排除同个属性不同类型的子类型

除了可以使用 Exclude<Animal, { legs: number }> 来创建 AnimalsWithFins 类型,该类型还可以通过 Exclude<Animal, { type: 'dog' | 'cat' }> 这种方式来创建。

type Animal =
    | { type: 'dog', legs: number }
    | { type: 'cat', legs: number }
    | { type: 'fish', fins: number };

type AnimalsWithFins = Exclude<Animal, { type: 'dog' | 'cat' }>;

const fish: AnimalsWithFins = { type: 'fish', fins: 6 }; // Ok
const dog: AnimalsWithFins = { type: 'dog', legs: 4 }; // Error
// Type '"dog"' is not assignable to type '"fish"'.

6.排除枚举类型的某些成员

利用 Exclude 工具类型可以排除枚举中的某些成员,从而得到一个新的类型。

enum Status { New, InProgress, Done, Cancelled }

type ActiveStatus = Exclude<Status, Status.Done | Status.Cancelled>;
// type ActiveStatus = Status.New | Status.InProgress

7.排除指定前缀的字符串字面量类型

利用 Exclude 工具类型和模板字面量类型,我们可以实现从字符串字面量联合类型中,排除指定前缀或后缀的字符串字面量。

type LogEvent =
    | "userLogin"
    | "userLogout"
    | "systemException"
    | "systemCrash"
    | "performanceLoadTime"
    | "performanceApiResponse";

type SystemAndPerformanceEvents = Exclude<LogEvent, `user${string}`>;
// type SystemAndPerformanceEvents = "systemException" | "systemCrash" | "performanceLoadTime" | "performanceApiResponse"

8.排除不同格式的字符串字面量类型

type LogEvent =
    | "userLogin"
    | "userLogout"
    | "UserLogin" // New
    | "UserLogout" // New
    | "systemException"
    | "systemCrash"
    | "performanceLoadTime"
    | "performanceApiResponse";

type SystemAndPerformanceEvents = Exclude<LogEvent, `${"user" | "User"}${string}`>;
// type SystemAndPerformanceEvents = "systemException" | "systemCrash" | "performanceLoadTime" | "performanceApiResponse"
责任编辑:姜华 来源: 全栈修仙之路
相关推荐

2024-03-21 09:58:27

ExtractTypeScript工具类型

2022-05-30 00:04:16

开源Github技巧

2012-10-29 11:01:17

2010-08-11 16:43:05

职场

2011-09-25 10:46:18

云计算安全

2010-08-25 11:14:05

云安全数据安全网络安全

2010-09-09 13:44:06

DIVCSS

2023-10-24 09:25:23

IT技巧文化

2023-11-29 10:16:45

内网开源

2022-12-15 16:38:17

2023-10-10 10:27:37

DevOps

2024-01-11 18:04:53

SQL数据库

2023-09-26 12:04:15

重构技巧Pythonic

2024-01-02 16:16:34

Promise前端

2023-01-11 11:35:40

重构PythonPythonic

2020-07-21 08:14:13

TypeScrip

2023-06-02 08:00:00

ChatGPT人工智能

2024-03-06 13:56:00

项目awaitpromise

2022-05-16 14:25:31

数据分析预测分析工具

2023-11-27 16:01:59

JavaScrip技巧
点赞
收藏

51CTO技术栈公众号