Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String enums from string constants #43902

Closed
5 tasks
t-ricci-enhancers opened this issue Apr 30, 2021 · 1 comment · Fixed by #50528
Closed
5 tasks

String enums from string constants #43902

t-ricci-enhancers opened this issue Apr 30, 2021 · 1 comment · Fixed by #50528
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Fix Available A PR has been opened for this issue Suggestion An idea for TypeScript

Comments

@t-ricci-enhancers
Copy link

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

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Apr 30, 2021
@t-ricci-enhancers
Copy link
Author

Expanding on https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html

You could also make enums like this:

enum BaseColor {
 BLUE = 'blue';
 //... more
}

enum DarkColor {
 DARK_BLUE = `${BaseColor.BLUE}_dark`
 //... more
}

@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Oct 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Fix Available A PR has been opened for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants