← Back to TypeScript Course | Chapter 8: Advanced Types | Lesson 16 of 20

Utility Types - Exclude और Extract

Exclude<T, U>, T से U को assignable members हटाता है। Extract<T, U> इसका opposite करता है सिर्फ U को assignable members रखते हुए।
Syntax
typescript
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

typescript
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

typescript
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

typescript
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

typescript
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

typescript
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);
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. दोनों को mix up करना: Exclude matching members हटाता है जबकि Extract उन्हें रखता है।
  2. Exclude को एक union के बजाय एक object type पर उपयोग करना, जब यह सिर्फ union members filter करता है।
  3. एक value जैसे "viewer" pass करना बजाय एक type के, यह भूलते हुए कि ये types पर काम करते हैं, values पर नहीं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.