In TypeScript, I want to compare two variables containing enum values. Here\'s my minimal code example:
enum E {
A,
B
}
let e1: E = E.A
let e2: E = E.B
if (
The error is thrown because the compiler realizes that the statement is always false and therefore redundant. You declare two variables which are clearly not equal and then try and see whether they are equal.
If you change it to e.g.:
enum E {
A,
B
}
foo() {
let e1: E = E.A
let e2: E
e2 = foo();
if (e1 === e2) {
console.log("equal")
}
}
bar(): E {
return E.B
}
it should compile without an error.
On a sidenote, sth. like
let e1 = E.A;
if (e1 && e1 === E.B) {
...
}
would also not compile, as e1
in this case is 0
(as A is the first enum 'option') and therefore false
which means that the second state would never be reached (disregarding whether the second statement would even be valid in this case)