never का Type
In this page:
function name(): never {
throw new Error("message");
}
Function जो Throw करता है
एक function जो हमेशा एक exception throw करता है असल में किसी भी circumstance में कभी एक value return नहीं करता, इसलिए TypeScript इसे void के बजाय never return type दे सकती है।
यह precisely यह capture करता है कि control flow normally उस function call के बाद कभी continue नहीं होता।
उदाहरण: Function That Throws
function fail(message: string): never {
throw new Error(message);
}
try {
fail("Something went wrong");
} catch (e) {
console.log((e as Error).message);
}
Infinite Loops
कोई break condition न वाली एक infinite loop रखने वाले एक function को भी never return करने वाला typed किया जा सकता है, क्योंकि execution असल में कभी एक return statement तक नहीं पहुंचता। TypeScript का control-flow analysis इस pattern को recognize करने के लिए काफी smart है।
उदाहरण: Infinite Loops
function logForever(): never {
let i = 0;
while (true) {
console.log(i);
if (i > 2) throw new Error("stopping demo loop");
i++;
}
}
try {
logForever();
} catch (e) {
console.log("loop ended");
}
Union Types में Never
never type एक union type में combine होने पर effectively disappear हो जाता है, क्योंकि यह zero possible values represent करता है और union क्या हो सकता है इसमें कुछ भी contribute नहीं करता।
यह naturally तब दिखता है जब TypeScript एक union के हर case को narrow करके away कर देती है, कुछ भी बचा न रहने देते हुए।
उदाहरण: Never in Union Types
type Combined = string | never; // never contributes nothing
let value: Combined = "hello";
console.log(value);
Exhaustive Checking
never exhaustive checking की backbone है: एक default branch जो अपने type के रूप में never expect करता है एक compiler error produce करेगा अगर एक union में एक नया member add किया जाए और unhandled छोड़ दिया जाए।
यह एक missed case को एक silent runtime bug के बजाय एक build-time failure में बदल देता है।
उदाहरण: Exhaustive Checking
type Shape = "circle" | "square";
function area(shape: Shape): number {
switch (shape) {
case "circle":
return 3.14;
case "square":
return 4;
default:
const exhaustive: never = shape;
return exhaustive;
}
}
console.log(area("circle"));
Never बनाम Void
void और never similar sound करते हैं लेकिन इनका मतलब बहुत अलग है: void का मतलब है एक function normally complete होता है बिना एक useful value return किए, जबकि never का मतलब है function normally बिल्कुल complete नहीं होता, चाहे throw करके या forever loop करके।
उदाहरण: Never vs Void
function completesNormally(): void {
console.log("Finished, but returns nothing useful");
}
function neverCompletes(): never {
throw new Error("Never returns");
}
completesNormally();
try { neverCompletes(); } catch (e) { console.log("caught"); }
- Normally return करने वाले एक function को
nevertyped करना, जबneverसिर्फ उन functions के लिए है जो throw करते हैं या कभी नहीं खत्म होते। fail()जैसे एक function के लिएneverके बजायvoidउपयोग करना जो हमेशा throw करता है, इसलिए compiler इसके बाद के code को unreachable नहीं मान सकता।- एक switch के
defaultbranch मेंnevercheck छोड़ देना, इसलिए एक newly added union member catch नहीं होता।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: