Last active
August 24, 2022 16:57
-
-
Save aldendaniels/ca041dc42bfe40afaa30f43511e39a63 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
enum Make { | |
Kia = 'Kia', | |
Ford = 'Ford', | |
Honda = 'Honda' | |
} | |
interface BaseCar { | |
make: Make; | |
color: string; | |
} | |
interface KiaCar extends BaseCar { | |
make: Make.Kia; | |
kiaSomethingSpecial: string; | |
} | |
interface FordCar extends BaseCar { | |
make: Make.Ford; | |
fordSomethingSpecial: string; | |
} | |
interface HondaCar extends BaseCar { | |
make: Make.Honda; | |
hondaSomethingSpecial: string; | |
} | |
type Car = KiaCar | FordCar | HondaCar; | |
function ensureUnreachable(value: never): never { | |
throw "Reached unreachable code"; | |
} | |
function drive(car: Car) { | |
switch(car.make) { | |
case Make.Kia: | |
console.log(car.kiaSomethingSpecial); | |
break; | |
case Make.Honda: | |
break; | |
case Make.Ford: | |
break; | |
default: | |
ensureUnreachable(car); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment