-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Const enum values as array #41122
Comments
Duplicate of #38960.
It wouldn't, tho.
Besides, you can just use |
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. |
We mostly agree and aren't inclined to dig that particular hole any deeper. |
This issue has been marked as "Out of Scope" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Workaround (if you really need const enum Test {
A = 'A',
B = 'B',
// C = 'C', /* uncomment the line to see compilation error */
}
type TestMap<T> = { [key in Test]: T };
const testUtilityMap: TestMap<null> = { [Test.A]: null, [Test.B]: null };
const allTests: ReadonlyArray<Test> = Object.freeze(Object.keys(testUtilityMap) as Array<Test>);
console.log(allTests.join(", ")); When you add a new item into enum, compiler will provide |
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:
It will compile into:
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:
The text was updated successfully, but these errors were encountered: