← Back to TypeScript Course | Chapter 5: Type Aliases and Union Types | Lesson 5 of 9

Discriminated Unions की जानकारी

एक discriminated union object types का एक union है जो अलग-अलग literal values वाली एक common property share करते हैं। वह property, जिसे discriminant कहा जाता है, TypeScript को exact object type सुरक्षित रूप से identify करने में मदद करती है।
Syntax
typescript
type Shape =
  | { kind: "a"; propA: type }
  | { kind: "b"; propB: type };

function name(shape: Shape) {
  switch (shape.kind) {
    case "a": /* ... */ break;
    case "b": /* ... */ break;
  }
}

Creating a Discriminated Union

एक discriminated union का हर member एक common property share करता है, conventionally type या kind नाम से, पर हर member इसे एक अलग literal value देता है।

TypeScript उस shared discriminant property का उपयोग यह figure out करने के लिए करता है कि यह exactly union के किस member से deal कर रहा है।

उदाहरण: Creating a Discriminated Union

typescript
type Circle = { kind: "circle"; radius: number };
type Square = { kind: "square"; side: number };
type Shape = Circle | Square;
const shape: Shape = { kind: "circle", radius: 5 };
console.log(shape.kind);

Narrowing with the Discriminant

Discriminant property की value check करना, usually एक if statement या switch के साथ, पूरे object को corresponding union member तक narrow करता है।

उस narrowed branch के अंदर, TypeScript उन properties को access होने देता है जो specifically उस एक member से belong करती हैं, पूरे union से नहीं।

उदाहरण: Narrowing with the Discriminant

typescript
type Circle = { kind: "circle"; radius: number };
type Square = { kind: "square"; side: number };
function area(shape: Circle | Square): number {
  if (shape.kind === "circle") return 3.14 * shape.radius ** 2;
  return shape.side ** 2;
}
console.log(area({ kind: "square", side: 4 }));

Discriminated Unions for Actions

Discriminated unions एक application में distinct actions represent करने के लिए खासकर well-suited हैं, जैसे एक Redux-style reducer, जहां हर action अपना type value रखता है और सिर्फ वे data fields जो उस specific action के लिए relevant हैं।

यह action handling को type-safe और follow करना आसान दोनों रखता है।

उदाहरण: Discriminated Unions for Actions

typescript
type Increment = { type: "increment"; amount: number };
type Reset = { type: "reset" };
function reducer(action: Increment | Reset, state: number): number {
  if (action.type === "increment") return state + action.amount;
  return 0;
}
console.log(reducer({ type: "increment", amount: 5 }, 10));

Multiple Discriminated Members

एक discriminated union में ज़रूरत के हिसाब से कितने भी members हो सकते हैं, और हर एक को discriminant property के लिए अपनी unique literal value उपयोग करनी चाहिए।

जब तक हर value unique है, TypeScript किसी भी संख्या के possible cases के बीच reliably distinguish कर सकता है।

उदाहरण: Multiple Discriminated Members

typescript
type A = { kind: "a"; value: number };
type B = { kind: "b"; text: string };
type C = { kind: "c" };
function handle(x: A | B | C) {
  console.log(x.kind);
}
handle({ kind: "b", text: "hi" });

Discriminated Unions and Exhaustive Handling

जब एक discriminated union के हर member को explicitly handle कर लिया गया हो, TypeScript एक exhaustiveness check उपयोग करके confirm करने में मदद कर सकता है कि कुछ miss नहीं हुआ, जो अक्सर एक helper function के साथ implement होता है जिसका parameter never के रूप में typed हो।

अगर बाद में union में एक नया member जोड़ा जाए और unhandled छोड़ दिया जाए, यह pattern इसे एक silent runtime bug के बजाय एक compile error के रूप में surface करता है।

उदाहरण: Discriminated Unions and Exhaustive Handling

typescript
type Circle = { kind: "circle"; radius: number };
type Square = { kind: "square"; side: number };
function area(shape: Circle | Square): number {
  switch (shape.kind) {
    case "circle": return 3.14 * shape.radius ** 2;
    case "square": return shape.side ** 2;
    default:
      const exhaustive: never = shape;
      return exhaustive;
  }
}
console.log(area({ kind: "circle", radius: 2 }));
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. हर member में discriminant के लिए अलग property names उपयोग करना, तो narrowing काम नहीं करता।
  2. Discriminant को "circle" जैसे literal के बजाय एक general string type देना।
  3. एक switch में एक case भूल जाना, तो union का एक member silently unhandled रह जाता है।
🔒

Chapter Quiz — Complete all 9 topics to unlock

0/9 topics done

Complete these topics first:

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.