Extending string-based enums
See original GitHub issueBefore string based enums, many would fall back to objects. Using objects also allows extending of types. For example:
const BasicEvents = {
Start: "Start",
Finish: "Finish"
};
const AdvEvents = {
...BasicEvents,
Pause: "Pause",
Resume: "Resume"
};
When switching over to string enums, it"s impossible to achieve this without re-defining the enum.
I would be very useful to be able to do something like this:
enum BasicEvents {
Start = "Start",
Finish = "Finish"
};
// extend enum using "extends" keyword
enum AdvEvents extends BasicEvents {
Pause = "Pause",
Resume = "Resume"
};
Considering that the produced enums are objects, this won"t be too horrible either:
// extend enum using spread
enum AdvEvents {
...BasicEvents,
Pause = "Pause",
Resume = "Resume"
};
Issue Analytics
- State:
- Created 6 years ago
- Reactions:816
- Comments:84 (13 by maintainers)
Top Results From Across the Web
How to extend enums in TypeScript - LogRocket Blog
The short answer is no, you can't extend enums because TypeScript offers no language feature to extend them. However, there are workarounds you ......
Read more >Extending Enum in typescript - Stack Overflow
I've just stumbled across this post from 2018 that explains how to do it with string based enums. See [1] for the original...
Read more >Extending Enums in Java - Baeldung
In this tutorial, we'll discuss extending enums in Java, including adding new constant values and new functionalities.
Read more >Handbook - Enums - TypeScript
String enums are a similar concept, but have some subtle runtime differences as documented below. In a string enum, each member has to...
Read more >Can we extend an enum in Java? - Tutorialspoint
No, we cannot extend an enum in Java. Java enums can extend java.lang.Enum class implicitly, so enum types cannot extend another class.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Just use class instead of enum.
All workarounds are nice but I would like to see the enum inheritance support from typescript itself so that I can use exhaustive checks as simple as possible.