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

Custom Type Guards की जानकारी

एक custom type guard एक function है जो TypeScript को बताता है कि क्या एक value का एक particular type है। value is User जैसा एक predicate return type guard succeed होने के बाद TypeScript को value narrow करने देता है।
Syntax
typescript
function isType(value: unknown): value is TargetType {
  return /* boolean check */;
}

Basic Type Predicate

एक type predicate अपने return type annotation के रूप में parameterName is Type form उपयोग करता है — function body को एक boolean return करना चाहिए जो genuinely और accurately जांचे कि क्या value उस type से match करती है।

उदाहरण: Basic Type Predicate

typescript
function isString(value: unknown): value is string {
  return typeof value === "string";
}
const val: unknown = "hello";
if (isString(val)) console.log(val.toUpperCase());

Checking Object Shapes

Custom guards सबसे ज़्यादा काम आते हैं जब API responses जैसे external sources से आने वाले unknown objects के साथ काम करें — एक value को एक interface से match मानने से पहले जांचें कि हर required property असल में मौजूद है और उसका सही type है।

उदाहरण: Checking Object Shapes

typescript
interface User {
  id: number;
  name: string;
}
function isUser(value: any): value is User {
  return typeof value?.id === "number" && typeof value?.name === "string";
}
const data: any = { id: 1, name: "Ravi" };
if (isUser(data)) console.log(data.name);

Guards for Union Types

एक custom guard एक union type के members के बीच अंतर कर सकता है, जो मायने रखता है जब भी उस union में अलग variants अलग properties रखते हैं जिन्हें plain equality checks बता नहीं सकते।

उदाहरण: Guards for Union Types

typescript
type Shape = { kind: "circle"; radius: number } | { kind: "square"; side: number };
function isCircle(s: Shape): s is { kind: "circle"; radius: number } {
  return s.kind === "circle";
}
const s: Shape = { kind: "circle", radius: 5 };
if (isCircle(s)) console.log(s.radius);

Generic Type Guards

Type guards generically भी लिखे जा सकते हैं — एक generic guard उस input type के बीच relationship preserve कर सकता है जो उसे मिला और वह ज़्यादा specific type जिस तक यह narrow करता है।

उदाहरण: Generic Type Guards

typescript
function isOfType<T>(value: unknown, check: (v: unknown) => boolean): value is T {
  return check(value);
}
const val: unknown = 42;
if (isOfType<number>(val, (v) => typeof v === "number")) console.log(val + 1);

Using Guards in Arrays

Custom guards filter जैसे array methods में सीधे pass किए जाने पर खासतौर पर अच्छे काम करते हैं — TypeScript एक predicate-shaped guard function को पहचानता है और filtered result के लिए अपने आप एक narrower array type infer करता है।

उदाहरण: Using Guards in Arrays

typescript
const values: (string | null)[] = ["a", null, "b"];
function isString(v: string | null): v is string {
  return v !== null;
}
const strings = values.filter(isString);
console.log(strings);
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. value is string के बजाय boolean return करना, तो caller का type narrow नहीं होता।
  2. एक predicate लिखना जो झूठ बोलता है, जैसे return true या सिर्फ एक property जांचना, जो TypeScript को गलत types trust करवाता है।
  3. यह भूल जाना कि guard सिर्फ pass किए गए argument को narrow करता है, किसी दूसरे variable में store की गई copy को नहीं।

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.