🟦 TypeScript Type Guards

Type Guards help you safely narrow down types in conditional blocks, so TypeScript knows exactly what you're working with.

📚 Example Code: Using Type Guards

Console output will appear here...
💡 Custom type guard functions use the syntax value is Type to tell TypeScript about type narrowing.

🔧 Key Points

📦 Example: instanceof Type Guard

class Dog {
  bark() {
    console.log("Woof!");
  }
}

class Cat {
  meow() {
    console.log("Meow!");
  }
}

function speak(animal: Dog | Cat) {
  if (animal instanceof Dog) {
    animal.bark();
  } else {
    animal.meow();
  }
}

speak(new Dog());
speak(new Cat());