in Operator का Guard
In this page:
if ("property" in object) {
// object has property here
}
Basic in Operator Guard
in operator का form property in object है, और जब एक union में अलग-अलग object types के अलग-अलग property names हों, उनमें से एक property check करना TypeScript को value को उस union branch तक narrow करने देता है जिसके पास वह actually है।
उदाहरण: Basic in Operator Guard
type Cat = { meow: () => void };
type Dog = { bark: () => void };
function speak(pet: Cat | Dog) {
if ("meow" in pet) {
pet.meow();
} else {
pet.bark();
}
}
speak({ meow: () => console.log("Meow") });
Object Properties के साथ in Operator
in operator उपयोगी है जब object types कुछ properties share करते हैं लेकिन हर एक के पास additional properties हों जो दूसरों के पास नहीं — क्योंकि distinguishing property check करना अक्सर एक dedicated discriminant field पर भरोसा करने से simpler है।
उदाहरण: in Operator with Object Properties
type Circle = { radius: number };
type Square = { side: number };
function area(shape: Circle | Square): number {
if ("radius" in shape) {
return 3.14 * shape.radius ** 2;
}
return shape.side ** 2;
}
console.log(area({ side: 4 }));
Methods के साथ in Operator
in द्वारा checked property के पास data होना जरूरी नहीं; यह एक method name भी हो सकती है, जो उन interfaces के बीच narrow करने के लिए उपयोगी है जो capabilities जैसी shaped हैं (उदाहरण के लिए एक fetchData method check करना) बजाय plain data fields के।
उदाहरण: in Operator with Methods
type Flyer = { fly: () => void };
type Swimmer = { swim: () => void };
function move(entity: Flyer | Swimmer) {
if ("fly" in entity) {
entity.fly();
} else {
entity.swim();
}
}
move({ fly: () => console.log("Flying") });
Multiple Union Members के साथ in Operator
in operator को repeatedly उपयोग किया जा सकता है जब एक union में कई object types हों, per branch एक candidate property check करते हुए जब तक code ने value को एक single specific shape तक narrow न कर लिया हो।
उदाहरण: in Operator with Multiple Union Members
type A = { a: string };
type B = { b: string };
type C = { c: string };
function handle(value: A | B | C) {
if ("a" in value) console.log("A:", value.a);
else if ("b" in value) console.log("B:", value.b);
else console.log("C:", value.c);
}
handle({ c: "hi" });
Unknown Values के साथ in Operator
in operator unknown values narrow करने में भी मदद कर सकता है, लेकिन value को पहले confirm किया जाना चाहिए कि वह एक object है (उदाहरण के लिए typeof value === object && value !== null से) इससे पहले कि TypeScript इस पर एक property check allow करे।
उदाहरण: in Operator with Unknown Values
function printName(value: unknown) {
if (typeof value === "object" && value !== null && "name" in value) {
console.log((value as { name: string }).name);
}
}
printName({ name: "Ravi" });
meow in petquotes के बिना लिखना, जोmeowनाम के एक variable को lookup करता है बजाय string"meow"के।- एक property check करना जो एक से ज़्यादा union member पर मौजूद हो, इसलिए
intypes को separate नहीं करता और else branch ambiguous रहता है। inको एक ऐसी value पर उपयोग करना जोnullया एक primitive हो सकती हो, जो runtime पर एकTypeErrorthrow करता है।
- Type aliases एक type को नाम देते हैं, जबकि union (
|) और intersection (&) types अलग-अलग तरीकों से types को combine करते हैं। - Literal types और discriminated unions cases के एक fixed set को model करते हैं जिनके बीच compiler फर्क बता सकता है।
typeof,instanceof, और in operator से Type narrowing आपको एक branch के अंदर safely एक specific type उपयोग करने देता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: