Skip to content

Instantly share code, notes, and snippets.

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:
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