Type Guards

Useful for type checking at compile and run time:

function isFish(pet: Fish | Bird): pet is Fish {  return (pet as Fish).swim !== undefined;}

Usage:

const pet = getSmallPet(); if (isFish(pet)) {  pet.swim();} else {  pet.fly();}