Cast int to enum strings in Typescript

前端 未结 3 1496
-上瘾入骨i
-上瘾入骨i 2020-12-14 05:39

I get from a RESTful Service the following data:

[
  {
    \"id\": 42,
    \"type\": 0,
    \"name\": \"Piety was here\",
    \"description\": \"Bacon is tas         


        
3条回答
  •  孤街浪徒
    2020-12-14 06:27

    Enums in TypeScript are numbers at runtime, so message.type will be 0, 1, 2 or 3.

    To get the string value, you need to pass that number into the enum as an index:

    Type[0] // "Info"
    

    So, in your example, you'll need to do this:

    Type[message.type] // "Info" when message.type is 0
    

    Docs

提交回复
热议问题