const enum
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'];
Eliminates the need to either drop const enums or hardcode all the values in an array and remember to add any new enum member.
My suggestion meets these guidelines:
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 aconst enum. Retrieving the values for aconst enumkinda 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.
Most helpful comment
We mostly agree and aren't inclined to dig that particular hole any deeper.