Check if value exists in enum in TypeScript

前端 未结 8 1550
悲哀的现实
悲哀的现实 2020-11-29 17:38

I recieve a number type = 3 and have to check if it exists in this enum:

export const MESSAGE_TYPE = {
    INFO: 1,
    SUCCESS: 2,
    WARNING:         


        
8条回答
  •  借酒劲吻你
    2020-11-29 17:49

    enum ServicePlatform {
        UPLAY = "uplay",
        PSN = "psn",
        XBL = "xbl"
    }
    

    becomes:

    { UPLAY: 'uplay', PSN: 'psn', XBL: 'xbl' }
    

    so

    ServicePlatform.UPLAY in ServicePlatform // false
    

    SOLUTION:

    ServicePlatform.UPLAY.toUpperCase() in ServicePlatform // true
    

提交回复
热议问题