Created
July 18, 2022 23:09
-
-
Save rayrayzayzay/31f4d119dc0ea2bdeb3a0ac78287799f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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