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

FR: Add an option for generating string enum. #1032

Closed
Rin0107 opened this issue Dec 9, 2024 · 4 comments
Closed

FR: Add an option for generating string enum. #1032

Rin0107 opened this issue Dec 9, 2024 · 4 comments

Comments

@Rin0107
Copy link

Rin0107 commented Dec 9, 2024

TL;DR

I propose an option that generate string enum (instead of numeric enum).

For example, the current enum generation looks like this:

enum Role {
  UNSPECIFIED = 0,
  ADMIN = 1,
  INTERNAL_ADMIN = 2,
  CONSUMER = 3
}

With the proposed string enum:

enum Role {
  UNSPECIFIED = ‘UNSPECIFIED’,
  INTERNAL_ADMIN = ‘INTERNAL_ADMIN’
  CONSUMER = ‘CONSUMER’
}

Why

Numeric enum in TS has a problem. It is type unsafe.
It is allowed below access.

enum Role {
  UNSPECIFIED = 0,
  ADMIN = 1,
  INTERNAL_ADMIN = 2,
  CONSUMER = 3
}

console.log(Role[4]) // It is expected compile error but…
=> undefiend

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.

enum RoleByMyself {
  UNSPECIFIED = ‘UNSPECIFIED’,
  INTERNAL_ADMIN = ‘INTERNAL_ADMIN’
  CONSUMER = ‘CONSUMER’
}

console.log(RoleByMyself[‘INTERNAL_ADMIN’])
=> ‘INTERNAL_ADMIN’
console.log(RoleByMyself[‘foo’]) // As expected, compile error 

For reference, stephenh/ts-proto has this option stringEnums=true.

Thanks for reading!

@Rin0107 Rin0107 changed the title FR: Add an option for generating string enum to get benefit by TS type checking. FR: Add an option for generating string enum. Dec 9, 2024
@timostamm
Copy link
Member

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.

@Rin0107
Copy link
Author

Rin0107 commented Dec 9, 2024

Thanks for replying!

Ok, I got your description of behaves about index signature.
For notes, this post speaks for my opinion.

And I sincerely appreciate your kindness in asking my situation.
Tomorrow, I will provide a more detailed explanation of it in my next comment.

@timostamm
Copy link
Member

For notes, this post speaks for my opinion.

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.

@Rin0107
Copy link
Author

Rin0107 commented Dec 10, 2024

First, I'm sorry that I misunderstood that string enum will help my situation.
So, I close this issue.

Thank you for taking the time.

@Rin0107 Rin0107 closed this as completed Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants