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 :
class AppComponent {
public color: Colors;
ngOnInit(): void {
const Colors = { ...Color2, ...Color1 };
this.color = Colors.Red; // Colors.Green or Colors.Yellow or Colors.Blue
}
}
Stackblitz Here (Angular)
Stackblitz Here (Typescript)
Answer from Abolfazl Roshanzamir on Stack OverflowCurrently, 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 :
class AppComponent {
public color: Colors;
ngOnInit(): void {
const Colors = { ...Color2, ...Color1 };
this.color = Colors.Red; // Colors.Green or Colors.Yellow or Colors.Blue
}
}
Stackblitz Here (Angular)
Stackblitz Here (Typescript)
I've just stumbled across this post from 2018 that explains how to do it with string based enums (see the original comment).
The key seems to be to declare a type AND a const with the same name.
Here's an annotated / fruity version:
enum someEnum {
Apple = 'Apple',
Banana = 'Banana'
}
enum extendedEnum {
Pear = 'Pear',
Grape = 'Grape'
}
// The key seems to be to declare a type AND
// a const with the same name
type AllFruits = someEnum | extendedEnum;
const AllFruits = {...someEnum, ...extendedEnum};
let f: AllFruits = AllFruits.Grape;
The original poster (who, by all rights, seems to have been a contributor to TypeScript and knows what they're talking about) mentions that, using this method, you can't use something like AllFruits.Grape as a 'type literal', which means you can't do this:
// This error will appear:
// 'AllFruits' only refers to a type, but is being used as a namespace here.
interface FruitBowl {
fruit: AllFruits.Grape
}
but this can be fixed with (something quite ugly) like:
interface FruitBowl {
fruit: typeof AllFruits.Grape
}
I guess this is one of the 'type workarounds' that others have mentioned.
(All credit to https://github.com/alangpierce)
Extending enum
Is there a mechanism for extending enums?
You can't inherit from enums in TypeScript, but could using unions help?
enum State
{
State1,
State2
}
enum AnotherState
{
State1,
State2,
State3,
State4
}
const x: State | AnotherState = State.State1;
const y: State | AnotherState = AnotherState.State3; More on reddit.com Extending string-based enums
Allow "T extends enum" generic constraint
Videos
Or other options? Say I have this state enum:
enum State {
State1,
State2
}I want to use this in a base class:
class Base {
state: State
}now I want to extend this class and have the state descend from State so it needs to include State1 and State2, but I can add things to it (this doesn't work):
enum DerivedState extends State {//should include State1 and State2 through inheritance
State3,
State4
}And my extended class would redefine state property like this:
class Derived extends Base {
state: DerivedState
}How should I approach it?
You can't inherit from enums in TypeScript, but could using unions help?
enum State
{
State1,
State2
}
enum AnotherState
{
State1,
State2,
State3,
State4
}
const x: State | AnotherState = State.State1;
const y: State | AnotherState = AnotherState.State3;
So far, there is no way to merge enums and I don't know any language that does support it. Frankly, I don't think it's right thing to do The closest thing that I use is string literals and union types. In your case that would be
type State =
| "State1"
| "State2";
type DerivedState = State
| "State3"
| "State4"
The usage is not as convenient as with enums, however, the IntelliSense still works. Try to Ctrl+Space inside of quotes on line 8