← Back to TypeScript Course | Chapter 2: Basic Types | Lesson 10 of 11

never का Type

never type उन values को represent करता है जो कभी occur नहीं होती। यह आमतौर पर उन functions के लिए उपयोग होता है जो हमेशा एक error throw करते हैं या normally कभी finish नहीं होते, और type-safe code में exhaustive checks के लिए।
Syntax
typescript
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

typescript
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

typescript
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

typescript
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

typescript
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

typescript
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"); }
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. Normally return करने वाले एक function को never typed करना, जब never सिर्फ उन functions के लिए है जो throw करते हैं या कभी नहीं खत्म होते।
  2. fail() जैसे एक function के लिए never के बजाय void उपयोग करना जो हमेशा throw करता है, इसलिए compiler इसके बाद के code को unreachable नहीं मान सकता।
  3. एक switch के default branch में never check छोड़ देना, इसलिए एक newly added union member catch नहीं होता।
🔒

Chapter Quiz — Complete all 11 topics to unlock

0/11 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.