Skip to content
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

Closed
4 of 5 tasks
Harpush opened this issue Oct 15, 2020 · 5 comments
Closed
4 of 5 tasks

Const enum values as array #41122

Harpush opened this issue Oct 15, 2020 · 5 comments
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@Harpush
Copy link

Harpush commented Oct 15, 2020

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.
@MartinJohns
Copy link
Contributor

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.

@Harpush
Copy link
Author

Harpush commented Oct 15, 2020

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.

@RyanCavanaugh RyanCavanaugh added Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript labels Oct 15, 2020
@RyanCavanaugh
Copy link
Member

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.

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Out of Scope" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@theanurin
Copy link

theanurin commented Jan 15, 2025

Workaround (if you really need const enum)

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(", "));

Playground Link

When you add a new item into enum, compiler will provide Property '[Test.C]' is missing in type '{ A: null; B: null; }' but required in type 'TestMap<null>'.ts(2741)
So you do not miss to add it in testUtilityMap (and as side effect into allTests array)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants