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

[REQ] [typescript-angular] option for using object enums as const #20809

Open
daniel-sc opened this issue Mar 5, 2025 · 1 comment
Open

[REQ] [typescript-angular] option for using object enums as const #20809

daniel-sc opened this issue Mar 5, 2025 · 1 comment

Comments

@daniel-sc
Copy link
Contributor

Is your feature request related to a problem? Please describe.

Currently the (default) enum generation does not use the maximal assertiveness of enum objects.
More specifically the type of any enum value is always the union of all enum values - this could be more specific.
This would help catch bugs, e.g. by allowing switch exhaustive checks etc..

Current Enum generation:

export type MyEnum = 'valueA' | 'valueB';

export const MyEnum = {
    ValueA: 'valueA' as MyEnum,
    ValueB: 'valueB' as MyEnum,
};

Describe the solution you'd like

It would be better to generate this instead (possibly configurable):

export type MyEnum = typeof MyEnum [keyof typeof MyEnum];

export const MyEnum = {
    ValueA: 'valueA',
    ValueB: 'valueB',
} as const;

Describe alternatives you've considered

Additional context

Any feedback welcome, especially wether this should change the default enum generation or should be opt in via some configration option.

@daniel-sc
Copy link
Contributor Author

P.S.: just saw that other typescript generators already use this approach:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant