typescriptprotocol-buffers

How to represent nested array in google protocol buffer file (proto3 to gRPC)?


I have a structure like this in TypesScript:

interface RouteShape {
  type: string;
  coordinates: number[][];
}

That I have to put into a .proto file. I looked at the other similar questions on SO but nothing found there seems to work. I put this in the proto file:

message RouteShape {
  string type = 1;
  repeated RepeatedCoordinates coordinates = 2;

  message RepeatedCoordinates {
    repeated double coordinates = 1;
  }
}

But when it is translated to TS it an array of objects, each containing a coordinates prop that has an array value.

Any help is much appreciated.


Solution

  • The closest thing I could find is to have this code in the .proto file:

    syntax = "proto3";
    package simple;
    
    import "google/protobuf/struct.proto";
    
    message RouteShape {
      string type = 1;
      repeated google.protobuf.ListValue coordinates = 2;
    }
    

    that generates this in the .ts file:

    export const protobufPackage = "simple";
    
    export interface RouteShape {
      type: string;
      coordinates: Array<any>[]; // the same as: any[][]
    }
    

    This is as close as I could get to

    interface RouteShape {
      type: string;
      coordinates: number[][];
    }