How to compare Enums in TypeScript

后端 未结 9 2269
攒了一身酷
攒了一身酷 2021-02-06 20:14

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 (         


        
9条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-06 20:47

    Well I think I found something that works:

    if (e1.valueOf() === e2.valueOf()) {
      console.log("equal")
    }
    

    But I'm a bit surprised that this isn't mentioned anywhere in the documentation.

提交回复
热议问题