Skip to content

Instantly share code, notes, and snippets.

@balanza
Last active January 30, 2019 16:48
Show Gist options
  • Save balanza/0aab0bf388d5ea01a76cec1ab033bb62 to your computer and use it in GitHub Desktop.
Save balanza/0aab0bf388d5ea01a76cec1ab033bb62 to your computer and use it in GitHub Desktop.
// 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