Skip to content

Instantly share code, notes, and snippets.

@dliv
Created April 20, 2017 14:46
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 dliv/826ce3237666bf3100bd73286a95ccee to your computer and use it in GitHub Desktop.
Save dliv/826ce3237666bf3100bd73286a95ccee to your computer and use it in GitHub Desktop.
Flow - Exhaustive Switch
// from: http://ouicar.github.io/2016/08/08/exhaustive-switch.html
// modified to use Flow comments
/* ::
type Empty = 'empty type' & 'nothing there';
type t = 'a' | 'b';
*/
function unexpectedCase(impossible /* : Empty */) /* : void */ {
reportError(`Unexpected case ${impossible}`);
}
// This function will type-check if and only if we handle all the cases.
function toNumber(value /* : t */) /* : number */ {
switch (value) {
case 'a':
return 0;
case 'b':
return 1;
default:
unexpectedCase(value);
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment