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:

⭐ 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