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

Control Flow Analysis की जानकारी

Control flow analysis TypeScript को track करने देता है कि code conditions, returns, assignments, और दूसरे branches से गुज़रते हुए values का type कैसे बदलता है। यह compiler को union types अपने आप narrow करने देता है।
Syntax
typescript
function name(value: TypeA | TypeB) {
  if (typeof value === "string") {
    // value is narrowed to string here
  } else {
    // value is narrowed to the remaining type
  }
}

Narrowing with typeof

TypeScript conditional branches के अंदर एक primitive union type narrow करने के लिए typeof checks उपयोग करता है — एक if (typeof x === "string") block के अंदर, x को उस branch के बाकी हिस्से के लिए exactly string माना जाता है।

उदाहरण: Narrowing with typeof

typescript
function print(x: string | number) {
  if (typeof x === "string") {
    console.log(x.toUpperCase());
  } else {
    console.log(x.toFixed(2));
  }
}
print("hello");

Truthiness Narrowing

TypeScript एक if statement में truthiness के लिए एक value जांचकर null, undefined, false, 0, और दूसरी falsy possibilities को एक type से eliminate कर सकता है, बिना एक explicit typeof या instanceof check की ज़रूरत के।

उदाहरण: Truthiness Narrowing

typescript
function printLength(x: string | null | undefined) {
  if (x) {
    console.log(x.length);
  } else {
    console.log("no value");
  }
}
printLength("hello");
printLength(null);

Equality Narrowing

Equality checks भी types narrow करते हैं, खासतौर पर उपयोगी जब एक union value को एक specific literal से compare करें — if (status === "error") के बाद, TypeScript जानता है कि status उस branch में exactly literal type "error" है।

उदाहरण: Equality Narrowing

typescript
type Status = "success" | "error" | "pending";
function handle(status: Status) {
  if (status === "error") {
    console.log("Something went wrong");
  } else {
    console.log("Status:", status);
  }
}
handle("error");

Assignments and Control Flow

TypeScript plain assignments को control flow के हिस्से के रूप में analyze करता है, तो एक variable उसके current value के आधार पर narrow हो सकता है भले ही उसका declared type एक बहुत wider union हो।

उदाहरण: Assignments and Control Flow

typescript
let value: string | number;
value = "hello";
console.log(value.toUpperCase());
value = 42;
console.log(value.toFixed(1));

Narrowing Across Functions

Control flow analysis return statements और type guards के साथ मिलकर काम करता है — एक बार एक branch return या throw कर दे, TypeScript उसके बाद आने वाले code में type narrow करने के लिए उस fact को उपयोग कर सकता है।

उदाहरण: Narrowing Across Functions

typescript
function process(x: string | null) {
  if (x === null) {
    return;
  }
  console.log(x.toUpperCase());
}
process("hello");
process(null);
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. यह मान लेना कि narrowing हर जगह रहती है, जब यह callbacks के अंदर और कुछ cases में reassignments के बाद खो जाती है।
  2. Numbers या strings पर एक truthiness check उपयोग करना, जो valid 0 और "" भी हटा देता है।
  3. एक wide type से एक variable declare करना और एक अलग branch में reassign होने के बाद assigned literal के narrow रहने की उम्मीद करना।

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.