Created
July 18, 2022 23:13
-
-
Save rayrayzayzay/787cdde9ffda5b8a0d2a74531bc67339 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
type Dog = { | |
kind: "dog" // 👀 | |
numberOfLegs: number | |
collar: { | |
name: string | |
material: "leather" | "plastic" | |
} | |
} | |
type Cat = { | |
kind: "cat" // 👀 | |
numberOfLegs: number | |
microchip: { | |
id: string | |
name: string | |
} | |
} | |
type Pet = Dog | Cat | |
function getName(pet: Pet): string { | |
switch (pet.kind) { | |
case "dog": return pet.collar.name | |
case "cat": return pet.microchip.name | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment