Created
August 27, 2017 20:38
-
-
Save asolove/ff59b0ce95b355d864a9d78f306e5dc5 to your computer and use it in GitHub Desktop.
Exhaustiveness checking in flow
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
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