Is there a way to create an enum that is a subset of another enum?
Sometimes it would be useful to have an enum that is a subset of another Enum with the same values at run time but under a different name.
Are there better ways to handle this scenario?
TypeScript
enum Original {
value = "value",
other = "other"
}
enum Derived {
value = Original.value
}
const test: Original = Derived.value;
Generated JavaScript
"use strict";
var Original;
(function (Original) {
Original["value"] = "value";
Original["other"] = "other";
})(Original || (Original = {}));
var Derived;
(function (Derived) {
Derived["value"] = "value";
})(Derived || (Derived = {}));
const test = Derived.value;
It would be more convenient if instead of assigning a static constant to the Derived enum, the value was derived from the Original enum at run time.
Possibilities:
- Replacement: Due to being derived from an existing enum, compilation can replace the Derived values with the Original values. This eliminates the need to create a separate Derived object in the JavaScript output.
Example Replacement:
"use strict";
var Original;
(function (Original) {
Original["value"] = "value";
Original["other"] = "other";
})(Original || (Original = {}));
const test = Original.value;