AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

Exclude 工具類型八個使用技巧

來源: 責編: 時間:2024-04-02 17:23:37 191觀看
導讀Exclude 是 TypeScript 中內置的工具類型,它用于從一個聯合類型中排除掉你不希望包含的類型,生成一個新的類型。這個工具類型在日常開發中非常有用,它能夠幫助我們編寫類型安全的代碼和更好地實現代碼復用。/** * Exclud

1Jh28資訊網——每日最新資訊28at.com

Exclude 是 TypeScript 中內置的工具類型,它用于從一個聯合類型中排除掉你不希望包含的類型,生成一個新的類型。這個工具類型在日常開發中非常有用,它能夠幫助我們編寫類型安全的代碼和更好地實現代碼復用。1Jh28資訊網——每日最新資訊28at.com

/** * 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 工具類型來滿足不同的使用場景。1Jh28資訊網——每日最新資訊28at.com

1.排除指定的基本數據類型

type MyTypes = string | number | boolean;type StringOrNumber = Exclude<MyTypes, boolean>;let uid: StringOrNumber = "semlinker" // Okuid = 2024 // Okuid = 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" 屬性的子類型。1Jh28資訊網——每日最新資訊28at.com

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 }; // Okconst 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' }> 這種方式來創建。1Jh28資訊網——每日最新資訊28at.com

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 }; // Okconst dog: AnimalsWithFins = { type: 'dog', legs: 4 }; // Error// Type '"dog"' is not assignable to type '"fish"'.

6.排除枚舉類型的某些成員

利用 Exclude 工具類型可以排除枚舉中的某些成員,從而得到一個新的類型。1Jh28資訊網——每日最新資訊28at.com

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

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

利用 Exclude 工具類型和模板字面量類型,我們可以實現從字符串字面量聯合類型中,排除指定前綴或后綴的字符串字面量。1Jh28資訊網——每日最新資訊28at.com

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"

本文鏈接:http://www.tebozhan.com/showinfo-26-80891-0.htmlExclude 工具類型八個使用技巧

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 提高生產力!這10個Lambda表達式必須掌握,開發效率嘎嘎上升!

下一篇: 我想做獨立開發,該如何起步?

標簽:
  • 熱門焦點
Top