Last active
January 30, 2019 16:48
-
-
Save balanza/0aab0bf388d5ea01a76cec1ab033bb62 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// optimistic implementation: assume mobile.status will only have those two values | |
const printMobile = (mobile: Mobile) => { | |
if(mobile.status === 'confirmed') { | |
console.log('Your mobile is confirmed!') | |
} else { | |
console.warn('Your mobile is not confirmed!') | |
} | |
} | |
// defensive implementation: assume mobile.status can be whatever | |
const printMobileAlternative = (mobile: Mobile) => { | |
if(mobile.status === 'confirmed') { | |
console.log('Your mobile is confirmed!') | |
} else if(mobile.status === 'unconfirmed') { | |
console.warn('Your mobile is not confirmed!') | |
} else { | |
throw new Error(`illegal state! mobile.status=${mobile.status}`) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment