在TypeScript中,我想比较两个包含枚举值的变量.下面是我的最小代码示例:
enum E {
A,
B
}
let e1: E = E.A
let e2: E = E.B
if (e1 === e2) {
console.log("equal")
}
使用tsc
(v2.0.3)编译时,我遇到以下错误:
TS2365:运算符"=="不能应用于类型"E.A"和"E.B".
==
、!==
和!=
也一样.
4.19.3 The <, >, <=, >=, ==, !=, ===, and !== operators
这些运算符要求一种或两种操作数类型可分配给另一种.结果总是布尔基元类型.
这(我认为)解释了这个错误.但我怎样才能摆脱它呢?
Side note
I'm using the Atom editor with atom-typescript, and I don't get any errors/warnings in my editor. But when I run tsc
in the same directory I get the error above. I thought they were supposed to use the same tsconfig.json
file, but apparently that's not the case.