自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Exclude 工具類型八個(gè)使用技巧

開(kāi)發(fā) 開(kāi)發(fā)工具
本文我將介紹 Exclude? 工具類型的 8 個(gè)使用技巧,掌握這些技巧之后,在工作中你就能更好地利用 Exclude 工具類型來(lái)滿足不同的使用場(chǎng)景。

Exclude 是 TypeScript 中內(nèi)置的工具類型,它用于從一個(gè)聯(lián)合類型中排除掉你不希望包含的類型,生成一個(gè)新的類型。這個(gè)工具類型在日常開(kāi)發(fā)中非常有用,它能夠幫助我們編寫(xiě)類型安全的代碼和更好地實(shí)現(xiàn)代碼復(fù)用。

/**
 * 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 個(gè)使用技巧,掌握這些技巧之后,在工作中你就能更好地利用 Exclude 工具類型來(lái)滿足不同的使用場(chǎng)景。

1.排除指定的基本數(shù)據(jù)類型

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.排除兩個(gè)聯(lián)合類型的共有成員

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

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

4.排除含有特定屬性的子類型

Animal 聯(lián)合類型,包含了多種動(dòng)物的描述對(duì)象,我們想從中排除含有 "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.排除同個(gè)屬性不同類型的子類型

除了可以使用 Exclude<Animal, { legs: number }> 來(lái)創(chuàng)建 AnimalsWithFins 類型,該類型還可以通過(guò) Exclude<Animal, { type: 'dog' | 'cat' }> 這種方式來(lái)創(chuàng)建。

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 工具類型可以排除枚舉中的某些成員,從而得到一個(gè)新的類型。

enum Status { New, InProgress, Done, Cancelled }

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

7.排除指定前綴的字符串字面量類型

利用 Exclude 工具類型和模板字面量類型,我們可以實(shí)現(xiàn)從字符串字面量聯(lián)合類型中,排除指定前綴或后綴的字符串字面量。

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"
責(zé)任編輯:姜華 來(lái)源: 全棧修仙之路
相關(guān)推薦

2024-03-21 09:58:27

ExtractTypeScript工具類型

2022-05-30 00:04:16

開(kāi)源Github技巧

2012-10-29 11:01:17

2022-12-15 16:38:17

2023-10-10 10:27:37

DevOps

2024-01-11 18:04:53

SQL數(shù)據(jù)庫(kù)

2025-04-03 08:27:00

Python代碼開(kāi)發(fā)

2024-03-06 13:56:00

項(xiàng)目awaitpromise

2010-08-11 16:43:05

職場(chǎng)

2011-09-25 10:46:18

云計(jì)算安全

2023-10-24 09:25:23

IT技巧文化

2025-01-02 15:08:36

SpringBoot自動(dòng)配置Java

2025-02-07 15:01:49

Promise數(shù)組前端

2010-08-25 11:14:05

云安全數(shù)據(jù)安全網(wǎng)絡(luò)安全

2010-09-09 13:44:06

DIVCSS

2022-05-16 14:25:31

數(shù)據(jù)分析預(yù)測(cè)分析工具

2023-11-29 10:16:45

內(nèi)網(wǎng)開(kāi)源

2023-01-03 11:47:47

2023-02-06 12:00:00

重構(gòu)PythonPythonic

2010-09-01 13:55:14

CSS
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)