protobuf.js
protobuf.js copied to clipboard
[pbts] oneof should use discriminating union types
protobuf.js version: 6.11.x
Given a proto
message Command {
oneof body {
string error = 1;
string result = 2;
}
}
It should output
type ICommand =
| {
body: 'error';
error: string;
}
| {
body: 'result'
result: string;
}
Currently it does the following
interface ICommand {
body: 'error' | 'result';
error?: string | null;
result?: string | null;
}
I think this becomes quite complex if there are multiple oneof
fields on a message.
I think ideally the discriminated union should be inside the body
property, and would need to have a form that was something like
type OneOfChoice<Type extends string, T> = { type: Key, value: T }
Which would result in something like
interface ICommand {
body: OneOfChoice<'error', {error: string}> | OneOfChoice<'result', {result: string}>
}