Skip to content

Instantly share code, notes, and snippets.

@asolove
Created August 27, 2017 20:38
Show Gist options
  • Save asolove/ff59b0ce95b355d864a9d78f306e5dc5 to your computer and use it in GitHub Desktop.
Save asolove/ff59b0ce95b355d864a9d78f306e5dc5 to your computer and use it in GitHub Desktop.
Exhaustiveness checking in flow
const checkType = (element: 'a' | 'b' | 'c'): string => {
switch (element) {
case 'a':
return 'first';
case 'b':
return 'second';
default:
(element: empty) // This cast only succeeds if you've already handled every possible case. Otherwise, you will get a type error
throw new Error()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment