Extending string-based enums
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"
};
Just played with it a little bit and it is currently possible to do this extension using an object for the extended type, so this should work fine:
enum BasicEvents {
Start = "Start",
Finish = "Finish"
};
// extend enum using "extends" keyword
const AdvEvents = {
...BasicEvents,
Pause: "Pause",
Resume: "Resume"
};
Note, you can get close with
enum E {}
enum BasicEvents {
Start = 'Start',
Finish = 'Finish'
}
enum AdvEvents {
Pause = 'Pause',
Resume = 'Resume'
}
function enumerate<T1 extends typeof E, T2 extends typeof E>(e1: T1, e2: T2) {
enum Events {
Restart = 'Restart'
}
return Events as typeof Events & T1 & T2;
}
const e = enumerate(BasicEvents, AdvEvents);
Another option, depending on your needs, is to use a union type:
const enum BasicEvents {
Start = 'Start',
Finish = 'Finish'
}
const enum AdvEvents {
Pause = 'Pause',
Resume = 'Resume'
}
type Events = BasicEvents | AdvEvents;
let e: Events = AdvEvents.Pause;
Downside is you can't use Events.Pause; you have to use AdvEvents.Pause. If you're using const enums, this is probably ok. Otherwise, it might not be sufficient for your use case.
We need this feature for strongly typed Redux reducers. Please add it in TypeScript.
Another workaround is to not use enums, but use something that looks like an enum:
const BasicEvents = {
Start: 'Start' as 'Start',
Finish: 'Finish' as 'Finish'
};
type BasicEvents = (typeof BasicEvents)[keyof typeof BasicEvents];
const AdvEvents = {
...BasicEvents,
Pause: 'Pause' as 'Pause',
Resume: 'Resume' as 'Resume'
};
type AdvEvents = (typeof AdvEvents)[keyof typeof AdvEvents];
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.
Just use class instead of enum.
I was just trying this out.
const BasicEvents = {
Start: 'Start' as 'Start',
Finish: 'Finish' as 'Finish'
};
type BasicEvents = (typeof BasicEvents)[keyof typeof BasicEvents];
const AdvEvents = {
...BasicEvents,
Pause: 'Pause' as 'Pause',
Resume: 'Resume' as 'Resume'
};
type AdvEvents = (typeof AdvEvents)[keyof typeof AdvEvents];
type sometype<T extends AdvEvents> =
T extends typeof AdvEvents.Start ? 'Some String' :
T extends typeof AdvEvents.Finish ? 'Some Other String' :
T extends typeof AdvEvents.Pause ? 'Abc' :
T extends typeof AdvEvents.Resume ? 'Xyz' : never;
type r = sometype<typeof AdvEvents.Finish>;
There has got to be a better way of doing this.
Why isn't this a feature already? No breaking changes, intuitive behavior, 80+ people who actively searched for and demand this feature – it seems like a no-brainer.
Even re-exporting enum from a different file in a namespace is really weird without extending enums (and it's impossible to re-export the enum in a way it's still enum and not object and type):
import { Foo as _Foo } from './Foo';
namespace Bar
{
enum Foo extends _Foo {} // nope, doesn't work
const Foo = _Foo;
type Foo = _Foo;
}
Bar.Foo // actually not an enum

+1 Currently using a workaround, but this should be a native enum feature.
I skimmed through this issue to see if anyone has posed the following question. (Seems not.)
From OP:
enum BasicEvents {
Start = "Start",
Finish = "Finish"
};
// extend enum using "extends" keyword
enum AdvEvents extends BasicEvents {
Pause = "Pause",
Resume = "Resume"
};
Would people expect AdvEvents to be assignable to BasicEvents? (As is, for example, the case with extends for classes.)
If yes, then how well does that mesh with the fact that enum types are meant to be final and not possible to extend?
@masak great point. The feature people want here is definitely not like normal extends. BasicEvents should be assignable to AdvEvents, not the other way around. Normal extends refines another type to be more specific, and in this case we want to broaden the other type to add more values, so any custom syntax for this should probably not use the extends keyword, or at least not use the syntax enum A extends B {.
On that note, I did like the suggestion of spread for this from OP.
// extend enum using spread
enum AdvEvents {
...BasicEvents,
Pause = "Pause",
Resume = "Resume"
};
Because spreading already carries the expectation of the original being shallow-cloned into an unconnected copy.
BasicEventsshould be assignable toAdvEvents, not the other way around.
I can see how that could be true in all cases, but I'm not sure it should be true in all cases, if you see what I mean. Feels like it'd be domain-dependent and rely on the reason those enum values were copied over.
I thought about workarounds a little more, and working off of https://github.com/Microsoft/TypeScript/issues/17592#issuecomment-331491147 , you can do a little better by also defining Events in the value space:
enum BasicEvents {
Start = 'Start',
Finish = 'Finish'
}
enum AdvEvents {
Pause = 'Pause',
Resume = 'Resume'
}
type Events = BasicEvents | AdvEvents;
const Events = {...BasicEvents, ...AdvEvents};
let e: Events = Events.Pause;
From my testing, looks like Events.Start is correctly interpreted as BasicEvents.Start in the type system, so exhaustiveness checking and discriminated union refinement seem to work fine. The main thing missing is that you can't use Events.Pause as a type literal; you need AdvEvents.Pause. You can use typeof Events.Pause and it resolves to AdvEvents.Pause, though people on my team have been confused by that sort of pattern and I think in practice I'd encourage AdvEvents.Pause when using it as a type.
(This is for the case when you want the enum types to be assignable between each other rather than isolated enums. From my experience, it's most common to want them to be assignable.)
Another suggestion (even though it does not solve the original problem), how about using string literals to create a type union instead?
type BEs = "Start" | "Finish";
type AEs = BEs | "Pause" | "Resume";
let example: AEs = "Finish"; // there is even autocompletion
So, the solution to our problems could be this?
const BasicEvents = {
Start: "Start",
Finish: "Finish"
};
// { Start: string, Finish: string };
const BasicEvents = {
Start: "Start",
Finish: "Finish"
} as const
// { Start: 'Start', Finish: 'Finish' };
https://github.com/Microsoft/TypeScript/pull/29510
Extending enums should be a core feature of TypeScript. Just sayin'
@wottpal Repeating my question from earlier:
If [enums can be extended], then how well does that mesh with the fact that enum types are meant to be final and not possible to extend?
Specifically, it seems to me that the totality check of a switch statement over an enum value depends on the non-extensibility of enums.
@masak What? No, it doesn't! Since extended enum is a wider type and cannot be assigned to the original enum, you always know all the values of every enum you use. Extending in this context means creating a new enum, not modifying the old one.
enum A { a; }
enum B extends A { b; }
declare var a: A;
switch(a) {
case A.a:
break;
default:
// a is never
}
declare var b: B;
switch(b) {
case A.a:
break;
default:
// b is B.b
}