Skip to content

Instantly share code, notes, and snippets.

@jantimon
Created July 20, 2018 10:09
Show Gist options
  • Save jantimon/a27a30804fed5e72bf6c303609bfc4f2 to your computer and use it in GitHub Desktop.
Save jantimon/a27a30804fed5e72bf6c303609bfc4f2 to your computer and use it in GitHub Desktop.
Typescript Prevent Default Case
interface Box {
height: number;
width: number;
color: 'RED' | 'BLUE'
}
function logColor(box: Box) {
switch (box.color) {
case 'RED':
console.log(box, 'is red');
break;
case 'BLUE':
console.log(box, 'is blue');
break;
default:
return throwBadKind(box.color);
}
}
function throwBadKind(x: never): never {
throw new Error('Unknown type "' + (x as string) + "'");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment