Closed
Description
π Search Terms
infinity nan enum string
β Viability Checklist
- 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 isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β 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
-
What do you want to use this for?
To maintain compatibility with existing code. -
What shortcomings exist with current approaches?
Please refer to the suggestion provided. -
What workarounds are you using in the meantime?
Currently, there are no workarounds available except renaming enum members, which would introduce breaking changes.