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

I expect there to be a new enumeration type where the value of the enumeration type is equal to the key of the enumeration #60604

Closed
6 tasks done
yanguoliang opened this issue Nov 26, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@yanguoliang
Copy link

🔍 Search Terms

"new enumeration type", "new enum", "enum key value"

✅ Viability Checklist

⭐ Suggestion

I expect there to be a new enumeration type where the value of the enumeration type is equal to the key of the enumeration,

📃 Motivating Example

When I define an enumeration so that the key and value are the same string, I need to write as follows:

enum Action {
  EDIT = 'EDIT',
  ADD= 'ADD',
  DELETE = 'DELETE',
}

This way a lot of redundant code is written.

I expect there to be a new enumeration type where the value of the enumeration type is equal to the key of the enumeration,

enumStr Action {
  EDIT,
  ADD,
  DELETE,
}
console.log(Action.EDIT === 'EDIT') // true
console.log(Action.ADD === 'ADD') // true
console.log(Action.DELETE === 'DELETE') // true
console.log(Action.EDIT === 0) // false
console.log(typeof Action.EDIT) // string
console.log(typeof Action.ADD) // string
console.log(typeof Action.DELETE) // string

A new keyword enumStr is used here to define an enumeration. The key and value of the enumeration are the same string.

💻 Use Cases

  1. What do you want to use this for?
    answer: Simplified syntax,Keep the original enum syntax unchanged,from
enum Action {
  EDIT = 'EDIT',
  ADD= 'ADD',
  DELETE = 'DELETE',
}

to

enumStr Action {
  EDIT,
  ADD,
  DELETE,
}
  1. What shortcomings exist with current approaches?
    answer: I need to repeat a literal for key and value
@MartinJohns
Copy link
Contributor

MartinJohns commented Nov 26, 2024

Duplicate of #16464. Used search terms: enum string in:title

@jakebailey jakebailey added the Duplicate An existing issue was already created label Nov 26, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants