How to check the type is enum or not in typescript

前端 未结 4 1209
梦谈多话
梦谈多话 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:42

    As @NearHuscarl said there is no way to check that. However you can check if djson.role (string) is one of UserRole (enum) values:

    !!(Object.values(UserRole).find(enumValue => enumValue === djson.role)));
    

提交回复
热议问题