How to compare Enums in TypeScript

后端 未结 9 2267
攒了一身酷
攒了一身酷 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:44

    The only thing that worked for me (in typescript 2.2.1) was this:

    if (E[e1] === E[e2]) {
      console.log("equal")
    }
    

    This compares the strings representing the names (eg. "A" and "B").

提交回复
热议问题