Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheCubicleBuddha/d8242b0057b5c343c16ff1877d528984 to your computer and use it in GitHub Desktop.
Save TheCubicleBuddha/d8242b0057b5c343c16ff1877d528984 to your computer and use it in GitHub Desktop.
Showing how introducing a new type in the ITrafficSignal union will cause a runtime bug. And in this case, a deadly mistake.
type ITrafficLight = "red"
| "redBlinking" // <-- new case
| "green"
| "yellow"
| "yellowBlinking"; // <-- new case
// Can you spot the bug?
// You can skip ahead to the answer here: https://gist.github.com/TheCubicleBuddha/7a8854f244697b9894e41d44e1fc6967
function respondToTrafficSignal(signal: ITrafficLight): "stop" | "go" | "pause" {
if(signal === "red"){
return "stop";
} else if(signal === "yellow"){
return "pause";
} else {
return "go";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment