I am getting the error "Operator '!==' cannot be applied to types" in the comparison with enums, but not with integer values:
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) {
}
Aren't these two examples equivalent? What is the cause of the error?
Update
Actually it helps to consider the equivalent && expression:
if (!(x === E.A && x === E.B)) {
}
if (!(x === 0 || x === 1)) {
}
In the first example, compiler can infer that E.A and E.B are members of union types and cannot be equal, so gives the error, but it doesn't know (care) about integers.
xisE.A, it'll disallow comparisons with any other type that isn'tE.A.!(x === E.A && x === E.B)still makes no sense.xcannot be identical toE.AandE.Bat the same time.!(x === 1 && x === 1)also doesn't make sense, I wonder why TypeScript doesn't complain about it.(x !== 0 || x !== 1)should become!(x === 0 && x === 1), and not!(x === 0 || x === 1)as appears above (commenting rather than editing as I'm not 100% certain I didn't miss something...)