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:
-
TypeScript Enums are Terrible. Here's Why
-
Why TypeScript enums suck
-
TypeScript Enums are Bad // Alternatives to use
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:
Other related issues:
-
#4753 (Suggestion: Iterate through Enum values)
-
#2457 (Allow iterating through enum variants with for of)
-
#51150
-
#48193 (see the comment)
Suggestion
For now TS numeric enums are compiled with indexes so the result can be confusing at runtime:
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:
TypeScript Enums are Terrible. Here's Why
Why TypeScript enums suck
TypeScript Enums are Bad // Alternatives to use
My idea (not only my - see) is to use
enumerable: falsefor 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:
🔍 Search Terms
enums, enumerable, iteration
✅ Viability Checklist
My suggestion meets these guidelines:
Other related issues:
#4753 (Suggestion: Iterate through Enum values)
#2457 (Allow iterating through enum variants with for of)
#51150
#48193 (see the comment)