Description
Suggestion
For now TS numeric enums are compiled with indexes so the result can be confusing at runtime:
Object.keys(enum_); // "A", "B", 0, 1
The behaviour is unusual so many community members just suggest not use enums at all. It’s the key point because some really useful language feature became “forbidden” because no one understands how to use it correctly:
My idea (not only my - see) is to use enumerable: false
for indexes.
UPD. Some steps for string & numeric enums unification were already made for TS 5.0; the whole part of roadmap concerned the unification:
Unification Between Numeric and Literal enums
@ahejlsberg wrote some meaningful things:
The distinction between these two kinds of enums is subtle and has been the source of a fair amount of confusion over time.
🔍 Search Terms
enums, enumerable, iteration
✅ 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.