Skip to content

Const enum values as array #41122

Closed as not planned
Closed as not planned
@Harpush

Description

@Harpush

Search Terms

const enum

Suggestion

So i looked for a way to get all values from const enum without hardcoding them and stumbled upon #21391.
The idea is similar but with one important difference.
Right now when i declare a const enum and use it the compiler replaces the usage with the actual value. The suggestion is allowing the same for values.
So when i write:

const enum Test {
  A = 'A',
  B = 'B'
}
const test = Test.A;
const test2 = Test.values();

It will compile into:

const test = 'A';
const test2 = ['A', 'B'];

Use Cases

Eliminates the need to either drop const enums or hardcode all the values in an array and remember to add any new enum member.

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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions