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

Exhaustiveness Checking की जानकारी

Exhaustiveness checking यह सुनिश्चित करता है कि एक union का हर member handle हो। एक common pattern switch के default branch में एक never-returning helper उपयोग करना है।
Syntax
typescript
function name(value: Union): returnType {
  switch (value.kind) {
    case "a": return /* ... */;
    default:
      const _exhaustive: never = value;
      return _exhaustive;
  }
}

The never Type

never type उन values को represent करता है जो असल में कभी हो ही नहीं सकते, जिसका मतलब है कि never के रूप में typed एक function parameter किसी भी union member को detect करने के लिए जिसे आप handle करना भूल गए एक compile-time trap के रूप में उपयोग हो सकता है।

उदाहरण: The never Type

typescript
function assertUnreachable(x: never): never {
  throw new Error("Unhandled case: " + x);
}
type Status = "active" | "inactive";
function check(s: Status) {
  if (s === "active") return "on";
  if (s === "inactive") return "off";
  return assertUnreachable(s);
}
console.log(check("active"));

Exhaustive Switch Statements

Discriminated unions exhaustive switch statements के लिए ideal हैं, क्योंकि switch में हर case इसकी discriminant property से identified exactly एक specific object variant से correspond कर सकता है।

उदाहरण: Exhaustive Switch Statements

typescript
type Shape = { kind: "circle"; radius: number } | { kind: "square"; side: number };
function area(shape: Shape): number {
  switch (shape.kind) {
    case "circle": return Math.PI * shape.radius ** 2;
    case "square": return shape.side ** 2;
  }
}
console.log(area({ kind: "square", side: 3 }));

Exhaustive if Statements

Exhaustiveness बिना switch के भी check हो सकती है — if/else branches से हर known union member को narrow away करें, फिर जो बचा है वह एक ऐसे function को pass करें जो सिर्फ never accept करने के लिए typed हो।

उदाहरण: Exhaustive if Statements

typescript
function assertNever(x: never): never {
  throw new Error("Unexpected: " + x);
}
type Status = "on" | "off";
function toggle(s: Status): Status {
  if (s === "on") return "off";
  if (s === "off") return "on";
  return assertNever(s);
}
console.log(toggle("on"));

Exhaustiveness in Functions

जो branch impossible होना चाहिए (जैसे switch का default case) उससे never return करना यह guarantee करने में मदद करता है कि एक function genuinely union के हर member को handle करता है, सिर्फ उन्हें नहीं जो author को याद रहे।

उदाहरण: Exhaustiveness in Functions

typescript
type Status = "active" | "inactive";
function label(s: Status): string {
  switch (s) {
    case "active": return "Active";
    case "inactive": return "Inactive";
    default:
      const _exhaustive: never = s;
      return _exhaustive;
  }
}
console.log(label("active"));

Benefits of Exhaustiveness

Exhaustiveness checks union-based code को बहुत ज़्यादा सुरक्षित बनाते हैं जैसे-जैसे एक application evolve होता है — अगर बाद में union में एक नया member जोड़ा जाए, compiler तुरंत हर switch या if-chain को flag करता है जिसे update करने की ज़रूरत है।

उदाहरण: Benefits of Exhaustiveness

typescript
type Status = "active" | "inactive" | "pending";
function assertNever(x: never): never {
  throw new Error("Unhandled: " + x);
}
function label(s: Status): string {
  if (s === "active") return "Active";
  if (s === "inactive") return "Inactive";
  if (s === "pending") return "Pending";
  return assertNever(s);
}
console.log(label("pending"));
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. switch update किए बिना union में एक नया member जोड़ना, और एक default रखना जो कुछ नहीं करता, तो कोई error नहीं दिखता।
  2. assertUnreachable(x: never) को गलत variable pass करना, जैसे original union, जो never नहीं है।
  3. यह भूल जाना कि never check के साथ एक missing default सिर्फ तब error देता है जब एक case असल में unhandled हो, न कि जब unrelated code बदले।

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.