-
Notifications
You must be signed in to change notification settings - Fork 172
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
Handling of enums #156
Comments
Hi @mattvagni, thank you for providing such a detailed pull request and volunteering to help with the fix. One of the goals of ts-protoc-gen is to provide TypeScript interfaces which match the javascript generated by the javascript protoc plugin. As a result I view this as a bug in our implementation (ie: we should not be generating enums, but instead an Looking at the generated code (eg: external_enum_pb.js), it looks like we should only provide a one-way mapping, ie: interface ExternalEnum {
DEFAULT: 0
FIRST: 1
SECOND: 2
} |
@jonnyreeves That makes sense - got mixed up in my solution, you are right: we don't want to support a two way mapping. Will have a go at doing that. Should be fairly straight-forward I think 👍 |
@jonnyreeves I'm not sure how to best define the messages. If you do this for example: export class EnumMessage extends jspb.Message {
- getInternalEnum(): EnumMessage.InternalEnum;
- setInternalEnum(value: EnumMessage.InternalEnum): void;
+ getInternalEnum(): EnumMessage.InternalEnum[keyof EnumMessage.InternalEnum];
+ setInternalEnum(value: EnumMessage.InternalEnum[keyof EnumMessage.InternalEnum]): void; to essentially say that you must essentially provide a 'value' of the proto's enum the authoring experience becomes a bit rubbish since IDEs basically just list out numbers like this: instead of this: It's definitely 'accurate' but i'm wondering if there is something we can do to make this nicer - any thoughts? |
More detail in improbable-eng#156
More detail in improbable-eng#156
Fixed in #157; open question around type-hints still stands if anyone knows a better way? |
HI @avishco1 This package only generates the types. You will still need to generate the actual javascript client (where the const will then actually be defined). You can do this using the javascript protoc plugin. |
@mattvagni @avishco1 Assuming I have:
And I generate both the JS and the TS using protoc, the only way I've figured out how to use use it with proper type safety is:
This works, however it's very clunky. Am I doing something wrong? Thanks. |
@doom-goober I don't think so. You can define type AnimalTypeValue = AnimalTypeMap[keyof AnimalTypeMap] and use that if you prefer. |
I've noticed something quite strange about the handling of enums, I'm not sure what the solution is but am happy to help contribute a fix once decided what the correct behaviour should be (and whether this is even an issue).
Assuming an enum in proto file like this:
The typescript generation will output:
The matching javascript output is:
The problem
The problem is that typescript enums should actually be outputted like so in javascript:
This resulting value of the
Emotion
var becomes this once the above code is evaluated:This allows for mapping in both directions so that both of these would be truthy:
If we treat the javascript output from protoc as correct it means that potentially this plugins' handling of proto enums should be different.
Potential solutions
1. Output type definitions that match the outputted javascript
I think this could be as simple as making enums be outputted like this in typescript:
2. Document this limitation of enums
We could very clearly say that this is the case in the documentation and the generated code?
Happy to propose/work on a solution once an approach has been decided upon and thanks for providing such a great library 👍
The text was updated successfully, but these errors were encountered: