Skip to content

Instantly share code, notes, and snippets.

@rayrayzayzay
Created July 18, 2022 23:09
Show Gist options
  • Save rayrayzayzay/31f4d119dc0ea2bdeb3a0ac78287799f to your computer and use it in GitHub Desktop.
Save rayrayzayzay/31f4d119dc0ea2bdeb3a0ac78287799f to your computer and use it in GitHub Desktop.
function isCat(pet: Pet): pet is Cat {
return "microchip" in pet
}
function isDog(pet: Pet): pet is Dog {
return "collar" in pet
}
function getName(pet: Pet): string {
if (isDog(pet)) return pet.collar.name
if (isCat(pet)) return pet.microchip.name
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment