-
Notifications
You must be signed in to change notification settings - Fork 349
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
Flat oneofs with type union #1145
Comments
Hi @deser ; at first I thought that was our default That might be easy-ish to add, since it is similar to the default/flat fields output, if you'd like to attempt a PR. Thanks! |
I'm afraid it's not that simple, consider nested oneof... syntax = "proto3";
message OuterMessage {
oneof outer_field {
InnerMessage inner_message = 1;
}
}
message InnerMessage {
oneof inner_field {
string string_value = 1;
int32 int_value = 2;
}
} I peeked at the code, seems a new chunk of code will need to be written there (I wouldn't be able to re-use existing logic handling |
What do you expect the type for your
Yep, that's how open source goes. :-) |
I think somehow like below: type OuterMessage = {} | {inner_message : {string_value: string}} | {inner_message : {int_value : number}} |
I was thinking it'd be something like:
Which I believe is "the same type" but would be a lot easier to output, given ts-proto's current "message oriented" output. |
Yes, there's also possibility to put shared props into its own common interface, but that might affect performance, so duplicating is probably ok |
FYI: For anyone landing here looking for a workaround, you can use this type to break N optional properties on an interface into a union of required (mutually exclusive) properties. type ExclusiveUnion<T> = NonNullable<{
[K in keyof T]: {
[P in K]-?: T[P];
}
}[keyof T]>
type Test = ExclusiveUnion<{ foo?: number, bar?: string }>; // { foo: number } | { bar: string } |
I'm not sure why you wouldn't put the Fwiw I would probably try to get the types to be:
Just to break things down a little bit, for when Fwiw @benlesh I don't totally follow your example, as I believe it assumes that |
Hey currently
oneof
have 3 variants and none of them produce correct type for me. I would need the true union to be generated. See the example below:Having
Generated TS will be:
The text was updated successfully, but these errors were encountered: