Why does a switch statement on an enum throw 'not comparable to type' error?
That's because the compiler already knows that the case temperature.hot
will never happen: the variable temp
is given the enum literal type temperature.cold
, which can only be assigned that value itself (or null if there are no strict null checks). As temperature.hot
is not a compatible value here, the compiler throws an error.
If we discard the information about the literal (by casting or retrieving the value from a function):
function how_cold(celsius: number): temperature {
return celsius > 40 ? temperature.hot : temperature.cold;
}
The code will then compile:
let temp = how_cold(35); // type is "temperature"
switch (temp) {
case temperature.cold:
console.log("Brrr....");
break;
case temperature.hot:
console.log("Yikes...")
break;
}
Alternatively, prepending +
to the value works because it converts the value to a number, which will also widen the type's scope and make it compatible with all enum variants, as well as other numbers.
let temp = temperature.cold;
switch (+temp) {
case temperature.cold:
console.log("Brrr....");
break;
case temperature.hot:
console.log("Yikes...")
break;
case 5:
console.log("What??");
break;
}
Another possible reason could be if you check if the enum variable is not null
but this also triggers if enumVal
is 0
. This only holds true if the enum has the default numeric values (so first item gets the value of 0
)
if (!!enumVal) {
switch (enumVal) {
case EnumClass.First // the if clause automatically excludes the first item
In my case the problem was as @Kris mentioned.
I had to add
if (icon !== null) {
switch (icon) {
or change the first value of the enum to be 1 instead of 0
export enum ICON_TYPE {
ICON_A = 1,
ICON_B = 2
}
This answer is very good however it does not give an example of casting. Here I use casting to disregard the compiler's knowledge of the constant debug_level
.
type DebugLevel = 'log' | 'warn' | 'error';
const debug_lvl: DebugLevel = 'warn';
switch((debug_lvl as DebugLevel)){
case 'error':
console.error(...args);
break;
case 'warn':
console.warn(...args);
break;
case 'log':
console.log(...args);
break;
}