Typescript: Const enum values as array

Created on 15 Oct 2020  路  3Comments  路  Source: microsoft/TypeScript

Search Terms

const enum

Suggestion

So i looked for a way to get all values from const enum without hardcoding them and stumbled upon https://github.com/microsoft/TypeScript/issues/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:

  • [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [x] This wouldn't change the runtime behavior of existing JavaScript code
  • [x] 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.)
  • [x] This feature would agree with the rest of TypeScript's Design Goals.
Out of Scope Suggestion

Most helpful comment

Well the whole const enum concept is against the goals then...

We mostly agree and aren't inclined to dig that particular hole any deeper.

All 3 comments

Duplicate of #38960.

[x] This feature would agree with the rest of TypeScript's Design Goals.

It wouldn't, tho.

Non-goal 5. Add or rely on run-time type information in programs, or emit different code based on the results of the type system. Instead, encourage programming patterns that do not require run-time metadata.


Besides, you can just use Object.values(..) when not using a const enum. Retrieving the values for a const enum kinda goes against the concept of it.

Duplicate of #38960.

[x] This feature would agree with the rest of TypeScript's Design Goals.

It wouldn't, tho.

Non-goal 5. Add or rely on run-time type information in programs, or emit different code based on the results of the type system. Instead, encourage programming patterns that do not require run-time metadata.

Besides, you can just use Object.values(..) when not using a const enum. Retrieving the values for a const enum kinda goes against the concept of it.

Well the whole const enum concept is against the goals then...

Well its still const enum values... Same as replace usage - replace values. Its not a runtime way its a compilation replacement like regular usage.

Well the whole const enum concept is against the goals then...

We mostly agree and aren't inclined to dig that particular hole any deeper.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bgrieder picture bgrieder  路  3Comments

DanielRosenwasser picture DanielRosenwasser  路  3Comments

dlaberge picture dlaberge  路  3Comments

remojansen picture remojansen  路  3Comments

manekinekko picture manekinekko  路  3Comments