Skip to content

Instantly share code, notes, and snippets.

@rayrayzayzay
Created July 18, 2022 23:13
Show Gist options
  • Save rayrayzayzay/787cdde9ffda5b8a0d2a74531bc67339 to your computer and use it in GitHub Desktop.
Save rayrayzayzay/787cdde9ffda5b8a0d2a74531bc67339 to your computer and use it in GitHub Desktop.
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