Skip to content

Instantly share code, notes, and snippets.

@TheCubicleBuddha
Last active April 19, 2019 15:02
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/351ba1fc924923c6f54949d3f6970a03 to your computer and use it in GitHub Desktop.
Save TheCubicleBuddha/351ba1fc924923c6f54949d3f6970a03 to your computer and use it in GitHub Desktop.
This is the non-defensive implementation. To see a way to prevent possible errors, check out the defensive implementation: https://gist.github.com/TheCubicleBuddha/7a8854f244697b9894e41d44e1fc6967
type ITrafficLight = "red" | "green" | "yellow";
// Can you see where the function below might go wrong in the future?
// 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