Extending Enum in typescript

Solution 1:

Currently, You can't extend enum in TypeScript

Another option is to use type:

enum Color1 {
  Red = "Red",
  Green = "Green"
}

enum Color2 {
  Yellow = "Yellow",
  Blue = "Blue"
}

define a new type named Colors :

type Colors = Color1 | Color2;

Then you can use it as below :

public color: Colors;

ngOnInit(): void {
  const Colors = { ...Color2, ...Color1 };
  this.color = Colors.Red; // Colors.Green or Colors.Yellow or Colors.Blue
}

Stackblitz Here

Solution 2:

You could use a union in your type:

enum abc {
  a = 1,
  b = 2,
  c = 3
}

enum def {
  d = 4,
  e = 5,
  f = 6
}

type abcdef = abc | def;

let x: abcdef;

x = abc.a;
x = def.d;