Last active
April 19, 2019 15:53
-
-
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.
This file contains 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
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