Update `oneof` representation
See original GitHub issueimport { getOneofValue } from '@protobuf-ts/runtime';
interface ISomeMessage {
foo: string;
}
type TSomeMessage = {
foo: string;
}
declare var oneofWithInterface: {
oneofKind: "a";
a: ISomeMessage;
} | {
oneofKind: undefined;
};
declare var oneofWithType: {
oneofKind: "a";
a: TSomeMessage;
} | {
oneofKind: undefined;
};
const t = getOneofValue(oneofWithType, 'a'); // TSomeMessage | undefined
const i = getOneofValue(oneofWithInterface, 'a'); // TS error
// Argument of type '{ oneofKind: "a"; a: ISomeMessage; } | { oneofKind: undefined; }' is not assignable to parameter of type 'UnknownOneofGroup'.
// Type '{ oneofKind: "a"; a: ISomeMessage; }' is not assignable to type 'UnknownOneofGroup'.
// Property 'a' is incompatible with index signature.
// Type 'ISomeMessage' is not assignable to type 'string | number | bigint | boolean | Uint8Array | UnknownMessage | undefined'.
// Type 'ISomeMessage' is not assignable to type 'UnknownMessage'.
// Index signature is missing in type 'ISomeMessage'.(2345)
The issue seems like some discrepancy between a type
and interface
and the use of index signatures. As shown above the TSomeMessage
type works just fine, but the identical interface causes issues. The issue can be solved in one of two ways:
- Change the code generation to output types for Messages. (this seems bad)
- Change
UnknownMessage
to be an interface. (this seems much better)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:19 (17 by maintainers)
Top Results From Across the Web
Language Guide (proto3) | Protocol Buffers - Google Developers
... Nested Types; Updating A Message Type; Unknown Fields; Any; Oneof; Maps; Packages ... In all cases, the value must fit in the...
Read more >How to represent a mix of enum and oneof in protobuf
I am trying to create a protocol buffer message with fields that are either a message or one of a choice of some...
Read more >oneOf / anyOf / allOf - react-jsonschema-form documentation
react-jsonschema-form supports custom widgets for oneOf, anyOf, and allOf. A schema with oneOf is valid if exactly one of the subschemas is valid....
Read more >Understanding JSON Schema 2020-12 documentation
oneOf : (XOR) Must be valid against exactly one of the subschemas. All of these keywords must be set to an array, where...
Read more >Update on the Cost and Quality of Defense Representation in ...
This report updates the 1998 Spencer Report. It includes revised commentary, endorsed by the Judicial Conference Committee on Defender Services, to the 1998 ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is kind of random, but I found a way to generically convert between the current oneof shape and the new one (see it in action: Playground):
This was implemented in https://github.com/timostamm/protobuf-ts/pull/262