Skip to content

Make enum indexes non-enumerable #53973

Closed as not planned
Closed as not planned
@svr93

Description

@svr93

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:

  1. TypeScript Enums are Terrible. Here's Why

  2. Why TypeScript enums suck

  3. 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:

  • 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.

Other related issues:

  1. #4753 (Suggestion: Iterate through Enum values)

  2. #2457 (Allow iterating through enum variants with for of)

  3. #51150

  4. #48193 (see the comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions