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