Accessing oneof dynamically
See original GitHub issueHi,
I can’t find a way nice way to access a oneof
property type dynamically, maybe you can advise
Imagine I have the following one of type:
export interface Task_Payload {
payload: {
oneofKind: "renameTask";
renameTask: RenameTask;
} | {
oneofKind: "noop";
noop: NoopTask;
} | {
oneofKind: "trimming";
trimming: TrimmingTask;
} | {
oneofKind: undefined;
};
}
I want to be able to access the type of whatever object has been received
const kind = type.payload.oneofKind;
if (!kind) {
throw new Error("No kind specified");
}
// it should be of the correct type or at least a
// union of RenameTask|TrimmingTask|NoopTask
const oneOfKind = type.payload[kind];
// The above doesn't work.
}
with the current type definition I’ll have to hard code each possible variant where i’d just like to store the value as is.
Any workaround for this still being type safe?
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (10 by maintainers)
Top Results From Across the Web
python - Dynamically access oneof value from a protobuf
Let's say I defined a protobuf message like this message Config { oneof config{ A a = 1 ...
Read more >Javascript: Dynamically get a 'oneof' type · Issue #4666 - GitHub
Hello, I'm trying to dynamically get a oneof type. Let's assume we have AWS resources, and I have them wrapped in another protobuf:...
Read more >Protobuf Any and Oneof fields for variant types - Microsoft Learn
Learn how to use the Any type and the Oneof keyword to represent variant ... Handling dynamic property types (that is, properties of...
Read more >Google::ProtocolBuffers::Dynamic - MetaCPAN
Protocol Buffers objects are normal blessed hashes, with auto-generated accessors for message fields. Accessing the hash directly is possible but not ...
Read more >Firebase Dynamic Links
Firebase Dynamic Links are links that work the way you want, on multiple platforms, and whether or not your app is already installed....
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
Thank you for looking into this so thoroughly, @jcready.
We already have
isOneofGroup()
. I’ve addedgetOneofValue()
,setOneofValue()
andclearOneofValue()
.They may be helpful in some obscure situations.
Yes, but doing this in the runtime is preferable because of less overall code size.