How to check the type is enum or not in typescript

前端 未结 4 1270
梦谈多话
梦谈多话 2021-01-23 13:23

I have a string enum type like:

export enum UserRole {
admin = "admin",
active = "active",
blocked = "blocked"
}

I

4条回答
  •  我在风中等你
    2021-01-23 13:35

    Javascript does not have the concept of Enum so it's not possible, When compiling, typescript translates the enum definition to normal Javascript object that you use everyday. No information about Enum is reserved for you to inspect.

    So this enum definition below

    enum UserRole {
        admin = "admin",
        active = "active",
        blocked = "blocked"
    }
    

    Will be translated to something like this

    var UserRole;
    (function (UserRole) {
        UserRole["admin"] = "admin";
        UserRole["active"] = "active";
        UserRole["blocked"] = "blocked";
    })(UserRole || (UserRole = {}));
    

提交回复
热议问题