← Back to TypeScript Course | Chapter 5: Type Aliases and Union Types | Lesson 9 of 9

in Operator का Guard

in operator check करता है कि क्या एक property किसी object पर मौजूद है। TypeScript इस check का उपयोग उन properties के आधार पर object types के एक union को narrow करने के लिए कर सकता है जो members के बीच फर्क करती हैं।
Syntax
typescript
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

typescript
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

typescript
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

typescript
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

typescript
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

typescript
function printName(value: unknown) {
  if (typeof value === "object" && value !== null && "name" in value) {
    console.log((value as { name: string }).name);
  }
}
printName({ name: "Ravi" });
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. meow in pet quotes के बिना लिखना, जो meow नाम के एक variable को lookup करता है बजाय string "meow" के।
  2. एक property check करना जो एक से ज़्यादा union member पर मौजूद हो, इसलिए in types को separate नहीं करता और else branch ambiguous रहता है।
  3. in को एक ऐसी value पर उपयोग करना जो null या एक primitive हो सकती हो, जो runtime पर एक TypeError throw करता है।
चैप्टर सारांश
  • 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:

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.