Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.