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

Assertion Functions की जानकारी

Assertion functions TypeScript को बताते हैं कि function सामान्य रूप से return होने के बाद एक condition guaranteed है। वे एक asserts return type उपयोग करते हैं और आगे बढ़ने से पहले inputs validate करने के लिए उपयोगी हैं।
Syntax
typescript
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

typescript
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

typescript
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

typescript
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

typescript
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

typescript
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");
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. asserts condition को एक normal boolean return से confuse करना, और function को एक if में उपयोग करना बजाय इसे एक statement के रूप में call करने के।
  2. Check fail होने पर function के अंदर throw न करना, तो code एक ऐसे type के साथ continue करता है जिसे TypeScript अब गलत तरीके से trust करता है।
  3. एक assertion function को एक variable या property के through बिना एक explicit type annotation के call करना, जिसे TypeScript reject करता है।

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.