How to merge two enums in TypeScript

前端 未结 6 1766
無奈伤痛
無奈伤痛 2021-01-01 10:30

Suppose I have two enums as described below in Typescript, then How do I merge them

enum Mammals {
    Humans,
    Bats,
    Dolphins
}

enum Reptiles {
             


        
6条回答
  •  悲哀的现实
    2021-01-01 11:09

    I'd say the proper way to do it would be defining a new type:

    enum Mammals {
        Humans = 'Humans',
        Bats = 'Bats',
        Dolphins = 'Dolphins',
    }
    
    enum Reptiles {
      Snakes = 'Snakes',
      Alligators = 'Alligators',
      Lizards = 'Lizards',
    }
    
    type Animals = Mammals | Reptiles;
    

提交回复
热议问题