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"
};
@patriciavillela I meant no offense. I should not have called it sexual orientation and removed male/female. I was trying to create an example that was relatable -- I did it quickly and sloppy. I think I might have had it on my mind for news reasons. I am willing to update the original content in order to make it more correct.
Per discussion at #51310, we're officially putting this one in "Waiting for TC39" status. There's a proposal that's actively being developed which would potentially bring some sort of enum declaration form to JavaScript, and we'd like this to either proceed, or have the committee informally agree to indefinitely hold off on adding enum to JS in any form.
A good alternative is:
enum X {
one = "a",
two = "b"
}
enum Y {
three = "c",
four = "d"
}
const XY = { ...X, ...Y } as const;
type XY = (typeof XY)[keyof typeof XY];
which will give you a type/value combination that's effectively indistinguishable from a hypothetical enum XY { ...X, three = "c", four = "d" } declaration
A good alternative is...
I would say this is not a "good" alternative, as much as it is just that, an alternative. It's messy, sloppy and extremely hard if not impossible to remember, unless you're a pure TypeScript pro.
I don't get why:
enum B extends A {}
is not viable. It's simple, elegant, and consistent with the rest of the TypeScript syntax.
I don't get why:
enum B extends A {}is not viable. It's simple, elegant, and consistent with the rest of the TypeScript syntax.
@whatwhywhenandwho Well... (and sorry for repeating myself; this is well-covered in the comments above, but I realize there's a lot of them, so maybe repeating the message here might be beneficial)
...using extends as a keyword for enums sets exactly the wrong expectations, which then cannot be upheld with enums. The enum feature implies "these are the enum values, and nothing else" (closed world). The extends keyword implies "we use something as a base, and then add some more stuff" (open world). The exhaustiveness check of switch is also based on the closed-world assumption. Allowing any kind of extends semantics breaks that assumption. (Now consider using extends on an enum, but in a third-party module you neither control or even know about.)
Seeing as how the main reason for having enums is to enforce the closed-world assumption and to allow for the exhaustiveness check in switch, I am vehemently, categorically opposed to using extends in the way you propose.
what about enum B includes A { }? either way, are there plans to implement this functionality?
type IncludesType<T extends {[key: string]: string}, U extends {[key: string]: string}> = T & U;
function includes<T extends {[key: string]: string}, U extends {[key: string]: string}>(first: T, second: U): IncludesType<T, U> {
return { ...first, ...second };
}
enum X {
one = "a",
two = "b",
}
enum Y {
three = "c",
four = "d",
}
const XY = includes(X, Y);
/////
function createIncludesFunction<U extends {[key: string]: string}>(baseEnum: U) {
return <T extends {[key: string]: string}>(superset: T): IncludesType<T, typeof baseEnum> => {
return includes(superset, baseEnum);
}
}
const includesY = createIncludesFunction(Y)
const XY = includesY(X)
type XY = ValueOf<typeof Events> // from type-fest
...using
extendsas a keyword for enums sets exactly the wrong expectations, which then cannot be upheld with enums. The enum feature implies "these are the enum values, and nothing else" (closed world). Theextendskeyword implies "we use something as a base, and then add some more stuff" (open world). The exhaustiveness check ofswitchis also based on the closed-world assumption. Allowing any kind ofextendssemantics breaks that assumption. (Now consider usingextendson an enum, but in a third-party module you neither control or even know about.)
Thinking about it a little bit philosophically, inheritance between classes is already a pretty weird feature. :smile: It lets you create instances of your own class which are also instances or someone else's class! Like, what's up with that? 😮
But it's something we expect from classes, and sometimes even use in healthy, well-defined ways. (Like extending base classes provided by a framework.) It works.
With enums, it runs up against the closed-world expectation, which is why enums never allow you to (a) create new instances/values of the enum besides those originally specified, nor (b) create any kind of derived enum/sub-enum with its own new instances.
Hey guys, it's been a long time since there has been no activity here. I don't want to stir up a hornet's nest, but the problem is still relevant. Are there any plans to solve it?
Are there any plans to solve it?
@XIshArkIX You can read Ryan Cavanaugh's response above; the tl;dr is that there's now a tc39 proposal for enums in JavaScript/EcmaScript, and it's unlikely that anything will happen with TypeScript's enum feature before that proposal lands. I think that's very reasonable; in a way, TypeScript's original enum feature already somewhat exceeded TypeScript's mission, which includes erasing types and compiling down to JavaScript, but not adding/altering runtime behavior.
I should add that the tc39 proposal is still at Stage 1 ("Under consideration") as of this writing. At a guess, it will not be accepted into the EcmaScript specification anytime soon.
Meanwhile, I recommend Ryan's workaround:
enum X {
one = "a",
two = "b"
}
enum Y {
three = "c",
four = "d"
}
const XY = { ...X, ...Y } as const;
type XY = (typeof XY)[keyof typeof XY];
...which (a) works today, (b) should satisfy those who want the behavior or "extending" an enum, but (c) doesn't suffer from the problems an extend keyword would have (discussed at length above).