merge enum typescript 1

merge enum typescript

enum Mammals {
    Humans = 'Humans',
    Bats = 'Bats',
    Dolphins = 'Dolphins',
}

enum Reptiles {
  Snakes = 'Snakes',
  Alligators = 'Alligators',
  Lizards = 'Lizards',
}

const Animals = {
   ...Mammals,
   ...Reptiles,
}

type Animals = Mammals | Reptiles

Here is what the above code is Doing:
1. We’re creating a union type of Mammals and Reptiles
2. We’re creating a type alias called Animals that is the union type of Mammals and Reptiles
3. We’re creating an object called Animals that has the same keys as the union type

Similar Posts