How to compare Enums in TypeScript

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

    I would define values for Enum like this and compare with ===

    const enum AnimalInfo {
    Tiger = "Tiger",
    Lion = "Lion"
    }
    
    let tigerStr = "Tiger";
    
    if (tigerStr === AnimalInfo.Tiger) {
      console.log('true');
    } else {
      console.log('false');
    }
    

提交回复
热议问题