Exhaustiveness Checking की जानकारी
In this page:
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
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
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
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
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
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"));
switchupdate किए बिना union में एक नया member जोड़ना, और एकdefaultरखना जो कुछ नहीं करता, तो कोई error नहीं दिखता।assertUnreachable(x: never)को गलत variable pass करना, जैसे original union, जोneverनहीं है।- यह भूल जाना कि
nevercheck के साथ एक missingdefaultसिर्फ तब error देता है जब एक case असल में unhandled हो, न कि जब unrelated code बदले।
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