From: http://ouicar.github.io/2016/08/08/exhaustive-switch.html
Created
April 20, 2017 14:46
-
-
Save dliv/826ce3237666bf3100bd73286a95ccee to your computer and use it in GitHub Desktop.
Flow - Exhaustive Switch
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
// 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