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