Assertion Functions की जानकारी
In this page:
function assertName(value: unknown): asserts value is TargetType {
if (/* check fails */) throw new Error("message");
}
Basic Assertions
एक assertion function special asserts condition return type उपयोग करता है TypeScript को बताने के लिए कि, एक बार function सामान्य रूप से return हो जाए, वह condition अब बाकी code के लिए guaranteed true है।
उदाहरण: Basic Assertions
function assert(condition: boolean, msg: string): asserts condition {
if (!condition) throw new Error(msg);
}
const x: number | undefined = 5;
assert(x !== undefined, "x must be defined");
console.log(x + 1);
Asserting Object Types
एक assertion function asserts value is Type form भी उपयोग कर सकता है एक unknown value को एक specific interface तक narrow करने के लिए — अगर function throw नहीं करता, TypeScript trust करता है कि value के पास अब वह shape है।
उदाहरण: Asserting Object Types
interface User {
name: string;
}
function assertIsUser(value: any): asserts value is User {
if (typeof value?.name !== "string") throw new Error("Not a user");
}
const data: any = { name: "Ravi" };
assertIsUser(data);
console.log(data.name);
Assertion Functions and null
Assertions तब shine करते हैं जब भी एक value को code सुरक्षित रूप से continue करने से पहले बिल्कुल मौजूद होना चाहिए — required configuration जो load होनी चाहिए, एक DOM element जो page पर होने की उम्मीद है, या एक application invariant जो hold होना चाहिए।
उदाहरण: Assertion Functions and null
function assertExists<T>(value: T | null, msg: string): asserts value is T {
if (value === null) throw new Error(msg);
}
const config: { key: string } | null = { key: "value" };
assertExists(config, "config must be loaded");
console.log(config.key);
Assertions for Invariants
Assertions उन rules को enforce कर सकते हैं जो एक application की logic में true रहनी चाहिए — अगर एक invariant violate हो, तुरंत throw करना execution को ठीक उस point पर रोकता है जहां चीज़ें गलत हुईं, बजाय एक bad value को propagate होने देने के।
उदाहरण: Assertions for Invariants
function assertPositive(n: number): asserts n is number {
if (n <= 0) throw new Error("value must be positive, got " + n);
}
const balance = 10;
assertPositive(balance);
console.log("Balance ok:", balance);
Assertions vs Boolean Guards
एक boolean type guard true या false return करता है और caller को यह decide करने देता है कि आगे क्या करना है, जबकि एक assertion function इसकी condition fail होने पर throw करता है और अगर नहीं throw करता तो उसके बाद आने वाले code के लिए silently type narrow करता है।
उदाहरण: Assertions vs Boolean Guards
function isPositive(n: number): boolean {
return n > 0;
}
function assertPositive(n: number): asserts n is number {
if (n <= 0) throw new Error("must be positive");
}
console.log(isPositive(-1));
assertPositive(5);
console.log("passed assertion");
asserts conditionको एक normal boolean return से confuse करना, और function को एकifमें उपयोग करना बजाय इसे एक statement के रूप में call करने के।- Check fail होने पर function के अंदर throw न करना, तो code एक ऐसे type के साथ continue करता है जिसे TypeScript अब गलत तरीके से trust करता है।
- एक assertion function को एक variable या property के through बिना एक explicit type annotation के call करना, जिसे TypeScript reject करता है।
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