question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Extending string-based enums

See original GitHub issue

Before 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:open
  • Created 6 years ago
  • Reactions:816
  • Comments:84 (13 by maintainers)

github_iconTop GitHub Comments

241reactions
nOstapcommented, Sep 4, 2018

Just use class instead of enum.

241reactions
Xenya0815commented, May 8, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found