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

Extending enum #55114

Closed
5 tasks done
Vincent-Lavallee opened this issue Jul 23, 2023 · 1 comment
Closed
5 tasks done

Extending enum #55114

Vincent-Lavallee opened this issue Jul 23, 2023 · 1 comment

Comments

@Vincent-Lavallee
Copy link

Suggestion

Extending enums

πŸ” Search Terms

  • extend enum
  • enum
  • extending enum

βœ… Viability Checklist

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

I would like to be able to extend enums from other enums. something with a syntax like this ish..

enum PrimaryColors {RED = 'red', YELLOW = 'yellow', BLUE = 'blue'}
enum Colors { GREEN = 'green', PURPLE = 'purple' } extends PrimaryColors 

Maybe even include the Omit operator so we could technically omit a value when extending an enum.

πŸ“ƒ Motivating Example

It would simplify working with enums greatly. In many projects I've seen duplicated enum values, some weird work-around to make them work without duplicating values or I've even heard some people say straight up don't use them since they're not as powerfull as they are in other language.

πŸ’» Use Cases

If I take the example I mentionned on top eg :

enum PrimaryColors {RED = 'red', YELLOW = 'yellow', BLUE = 'blue'}
enum Colors { GREEN = 'green', PURPLE = 'purple' }

If I wanted color to looks something like this but still keep a PrimaryColor enum (to give better typing to places where it can only be a PrimaryColor)

enum Colors { GREEN = 'green', PURPLE = 'purple', RED = 'red', YELLOW = 'yellow', BLUE = 'blue' }

I would either need to duplicate values within the enum (which isn't great) or do something unusual like this :

type Colors = PrimaryColors | OtherColors;
const Colors = {...PrimaryColors , ...OtherColors};

let color: Colors = Colors.RED

I'm not the only one which has ran into this issue (see : https://stackoverflow.com/questions/52857698/extending-enum-in-typescript
). Enum kinda feel like pointless when you'd be better declaring Objects as const which is unfortunate IMO since they have syntax highlighthing and are usually easier to recognize.

@MartinJohns
Copy link
Contributor

Duplicate of #17592.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants