When comparing enums, I encountered a perplexing error message stating "Operator '!==' cannot be applied to types," whereas the same comparison with integer values did not result in an error:
enum E {
A,
B,
C,
D,
E,
F
}
let x: E
// Operator '!==' cannot be applied to types 'E.A' and 'E.B'
if (x !== E.A || x !== E.B) {
}
// OK
if (x !== 0 || x !== 1) {
}
Why aren't these two examples equivalent? What exactly is causing this error?
Update
I found that viewing the equivalent &&
expression can provide some clarity:
if (!(x === E.A && x === E.B)) {
}
if (!(x === 0 || x === 1)) {
}
The compiler seems to infer that E.A
and E.B
are members of union types that cannot be equal, hence triggering the error message. However, it overlooks such considerations when dealing with integers.