Skip to content

Encoding objects with oneof and undefined vs null #808

@SkeLLLa

Description

@SkeLLLa

protobuf.js version:6.7.3

Here is example proto file that describes message format that could contain one of some Types in body.

syntax = "proto3";

import "Type1.proto";
import "Type2.proto";
import "Type3.proto";
...
import "Type100500.proto";

package pack;

message MyMessage {
    string head = 1;
    oneof body {
        Type1 Type1 = 2;
        Type2 Type2 = 3;
        Type3 Type3 = 4;
...
        Type100500 Type100500 = 1005001;
    }
}

Proto files are compiled into js with pbjs.
Later in typescript:

import * as proto from 'my-static-protobuf-schemas';

const encode(type: string, payload: any): ArrayBuffer {
  const message = proto.pack.MyMessage.create({
      head: 'my-message-header',
      body: type,
      [type]: payload
    } as proto.pack.MyMessage$Properties);
  return proto.pack.MyMessage.encode(message).finish()
}

And here typescript throws error

Error:TS2345:Argument of type 'MyMessage' is not assignable to parameter of type 'MyMessage$Properties'.
  Types of property 'Type1' are incompatible.
    Type 'Type1$Properties | null' is not assignable to type 'Type1$Properties | undefined'.
      Type 'null' is not assignable to type 'Type1$Properties | undefined'.

Should I set to null all 100499 objects or there is some way to do that automatically?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions