Custom Type Guards की जानकारी
In this page:
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
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
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
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
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
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);
value is stringके बजायbooleanreturn करना, तो caller का type narrow नहीं होता।- एक predicate लिखना जो झूठ बोलता है, जैसे
return trueया सिर्फ एक property जांचना, जो TypeScript को गलत types trust करवाता है। - यह भूल जाना कि guard सिर्फ pass किए गए argument को narrow करता है, किसी दूसरे variable में store की गई copy को नहीं।
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