String enums from string constants · Issue #43902 · microsoft/TypeScript (original) (raw)
Suggestion
If possible, it would be nice to be able to define enum with string values, coping over string values from other enums (if their values can be calculated statically during compilation). Example:
export enum ColdColors { BLUE = '#0000FF', // ... }
export enum WarmColors { YELLOW = '#FFFF00', // ... }
export enum Colors { BLUE = ColdColors.BLUE, YELLOW = WarmColors.YELLOW, // ... }
// And/OR also
export const robinsEggBlue = '#00C0DE';
export const enum ColdColors2 { BLUE = '#0000FF', ROBINS_EGG_BLUE = robinsEggBlue, // ... }
export const enum WarmColors2 { YELLOW = '#FFFF00', // ... }
export const enum Colors2 { BLUE = ColdColors2.BLUE, YELLOW = WarmColors2.YELLOW, // ... }
🔍 Search Terms
List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.
✅ Viability Checklist
I DON'T know if my suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
📃 Motivating Example
As shown above, in the code example, this will help to avoid duplicating constant string literals everywhere (if you want to define them in multiple places, including inside an enum).
💻 Use Cases
As stated in the motivation, to group string literals that are defined as constants inside enums