I have some code:
enum Color {
Red,
Green,
Blue
}
function getColorName(c: Color): string {
switch(c) {
case Color.Red:
You don't need to use never or add anything to the end of your switch.
If
switch statement returns in each casestrictNullChecks typescript compilation flag turned onundefined or voidYou will get an error if your switch statement is non-exhaustive as there will be a case where nothing is returned.
From your example, if you do
function getColorName(c: Color): string {
switch(c) {
case Color.Red:
return 'red';
case Color.Green:
return 'green';
// Forgot about Blue
}
}
You will get the following compilation error:
Function lacks ending return statement and return type does not include
undefined.