Created
July 18, 2022 23:07
-
-
Save rayrayzayzay/7b6b7da293c1b59514798975c8f659d4 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 getName(pet: Pet): string { | |
if ("collar" in pet) { | |
// TypeScript knows that pet is a Dog now | |
return pet.collar.name | |
} | |
// TypeScript knows that it can only be a Cat now | |
return pet.microchip.name | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment