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

Allow Infinity, -Infinity and NaN as an enum key name for string enum #57845

Closed
6 tasks done
XeroAlpha opened this issue Mar 19, 2024 · 3 comments Β· Fixed by #57853
Closed
6 tasks done

Allow Infinity, -Infinity and NaN as an enum key name for string enum #57845

XeroAlpha opened this issue Mar 19, 2024 · 3 comments Β· Fixed by #57853
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@XeroAlpha
Copy link

XeroAlpha commented Mar 19, 2024

πŸ” Search Terms

infinity nan enum string

βœ… Viability Checklist

⭐ Suggestion

Allow the usage of Infinity, -Infinity, and NaN as key names in a string enum.

I understand the reasoning behind disallowing numeric names as enum keys, as they can cause conflicts with reverse mapping.
However, in the case of a string enum where every value is a string, reverse mapping is not applied. Therefore, it does not make sense to disallow numeric names in this scenario.

πŸ“ƒ Motivating Example

enum SomeEnum {
    Infinity = 1, // This should fail!
    Apple = Number.POSITIVE_INFINITY,
}

enum MinecraftEnchantmentTypes {
    Infinity = 'infinity', // This should pass!
    Unbreaking = 'unbreaking'
}

πŸ’» Use Cases

  1. What do you want to use this for?
    To maintain compatibility with existing code.

  2. What shortcomings exist with current approaches?
    Please refer to the suggestion provided.

  3. What workarounds are you using in the meantime?
    Currently, there are no workarounds available except renaming enum members, which would introduce breaking changes.

@MartinJohns
Copy link
Contributor

This was allowed before, but then it was intentionally removed again: #56161

@RyanCavanaugh
Copy link
Member

I don't think this rule makes sense to apply to string enums. Will discuss.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Mar 19, 2024
@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Mar 19, 2024

It's not purely about string enum members. It depends entirely on whether other members contribute a reverse mapping that could conflict:

Playground Link

enum E {
    Infinity = "boop",
    blah = 1/0,
}

console.log(E.Infinity); // prints "blah", not "boop"!

I other words, we need to keep the keys and reverse-mapping keys disjoint so one doesn't stomp on the other.

There's basically two ways of relaxing this:

  1. numerically named string enum members are allowed if all members of an enum are string-based
  2. numerically named string enum members are allowed if no other enum member contributes a conflicting reverse mapping

(1) is just a heavier restriction of (2) because it'll be vacuously true - there will never be any conflicting reverse mappings because there won't be any to begin with on an all-string enum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants