-
Notifications
You must be signed in to change notification settings - Fork 73
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
FR: Add an option for generating string enum. #1032
Comments
Hey Rin, regarding type-safety: TypeScript enums provide an index signature that behaves like any other index signature. For example: const arr: string[] = [];
const val: string = arr[1]; // actually undefined You can configure the behavior with the compiler option noUncheckedIndexedAccess. TypeScript are certainly not perfect, but I'm not sure this specific issue is the most problematic, or that it warrants switching to string enums - which don't even provide a mapping from numeric values at all. Regarding a BFF, can you provide more details on the situation? What format exactly are BFF and F using? I suspect that there's already a solution. |
Thanks for replying! Ok, I got your description of behaves about index signature. And I sincerely appreciate your kindness in asking my situation. |
I agree with the bundle size concerns, but the type-safety issue has been addressed with TypeScript 5. It will raise an error at compile time for the example, with standard compiler settings. |
First, I'm sorry that I misunderstood that string enum will help my situation. Thank you for taking the time. |
TL;DR
I propose an option that generate string enum (instead of numeric enum).
For example, the current enum generation looks like this:
With the proposed string enum:
Why
Numeric enum in TS has a problem. It is type unsafe.
It is allowed below access.
String enums can check it and occur compile error.
Then, in below use-case, only-numeric-enums-generate cause a problem.
I face it in a situation like that I generated ts code in BFF (Backend for Frontend) from Backend protobuf.
ClientSide and Backend expect string (e.g ‘ADMIN’), but BFF has only numeric enum, so BFF has to re-defien string enum by myself.
For reference, stephenh/ts-proto has this option stringEnums=true.
Thanks for reading!
The text was updated successfully, but these errors were encountered: