Utility Types - Exclude और Extract
In this page:
type Result1 = Exclude<UnionType, ExcludedType>;
type Result2 = Extract<UnionType, ExtractedType>;
Basic Exclude
Exclude<T, U> union T से हर वह member हटाता है जो U को assignable हो, सिर्फ T के वे members छोड़ते हुए जो U से बिल्कुल match नहीं करते। यह आमतौर पर literal values के एक बड़े union से कुछ unwanted options trim करने के लिए उपयोग होता है।
उदाहरण: Basic Exclude
type Role = "admin" | "editor" | "viewer";
type EditableRole = Exclude<Role, "viewer">;
const role: EditableRole = "editor";
console.log(role);
Basic Extract
Extract<T, U>, Exclude का opposite करता है — यह सिर्फ union T के वे members रखता है जो U को assignable हों, बाकी सब discard करते हुए। साथ में, Exclude और Extract दूसरे type के आधार पर एक union filter करने की दोनों directions cover करते हैं।
उदाहरण: Basic Extract
type Role = "admin" | "editor" | "viewer";
type ElevatedRole = Extract<Role, "admin" | "editor">;
const role: ElevatedRole = "admin";
console.log(role);
Exclude with Literal Unions
Exclude string या number literals के एक बड़े union से specific options हटाकर allowed literal values का एक छोटा set बनाने के लिए उपयोगी है, जैसे एक Status union से एक deprecated status value हटाना।
उदाहरण: Exclude with Literal Unions
type Status = "active" | "inactive" | "deprecated";
type CurrentStatus = Exclude<Status, "deprecated">;
const s: CurrentStatus = "active";
console.log(s);
Extract with Object Unions
Extract एक shared structural property के आधार पर एक union से members select कर सकता है, जो उपयोगी है जब union में कई object shapes हों और आपको सिर्फ वे चाहिए जो एक particular pattern से match करें।
उदाहरण: Extract with Object Unions
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; side: number }
| { kind: "line" };
type Filled = Extract<Shape, { kind: "circle" } | { kind: "square" }>;
const f: Filled = { kind: "circle", radius: 3 };
console.log(f);
Choosing Exclude or Extract
Exclude उपयोग करें जब आप एक बड़े set से specific union members हटाना चाहें, और Extract जब आप सिर्फ एक particular type से match करने वाले members रखना चाहें — दोनों utilities एक-दूसरे के natural complements हैं।
उदाहरण: Choosing Exclude or Extract
type Status = "active" | "inactive" | "banned";
type Removed = Exclude<Status, "active" | "inactive">;
type Kept = Extract<Status, "active" | "inactive">;
const removed: Removed = "banned";
const kept: Kept = "active";
console.log(removed, kept);
- दोनों को mix up करना:
Excludematching members हटाता है जबकिExtractउन्हें रखता है। Excludeको एक union के बजाय एक object type पर उपयोग करना, जब यह सिर्फ union members filter करता है।- एक value जैसे
"viewer"pass करना बजाय एक type के, यह भूलते हुए कि ये types पर काम करते हैं, values पर नहीं।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- TypeScript Advanced Types
- Mapped Types
- Conditional Types
- Custom Type Guards
- Assertion Functions
- Control Flow Analysis
- Exhaustiveness Checking
- Satisfies Operator
- Template Literal Types
- Utility Types - Partial
- Utility Types - Required
- Utility Types - Readonly
- Utility Types - Pick
- Utility Types - Omit
- Utility Types - Record
- Utility Types - Exclude and Extract
- Utility Types - NonNullable
- Utility Types - ReturnType
- Utility Types - Parameters
- TypeScript 5 Updates