Grpc-web: Missing some class definitions in the generated d.ts files.

Created on 7 Sep 2018  路  9Comments  路  Source: grpc/grpc-web

I was trying the new d.ts generation feature (thanks again for adding it) and I found that in the generated definition file some of the types are declared as Objects when they could have been strongly typed.
For instance, for the following .proto file:

syntax = "proto3";

service UserService{

    rpc GetUser (GetUserRequest) returns (GetUserResponse);
}

message GetUserRequest {
    string user_name = 1;
}

message GetUserResponse {
    User user = 1;
}

message User {
    string name = 1;
}

The produced _pb_d.ts file looks as follows:

export class GetUserRequest {
  constructor ();
  getUserName(): string;
  setUserName(a: string): void;
  serializeBinary(): Uint8Array;
  static deserializeBinary: (bytes: {}) => GetUserRequest;
}

export class GetUserResponse {
  constructor ();
  getUser(): {};
  setUser(a: {}): void;
  serializeBinary(): Uint8Array;
  static deserializeBinary: (bytes: {}) => GetUserResponse;
}

The getUser() method returns {} when it could be User. The export class User is missing as well. I was expecting something like this:

export class GetUserRequest {
  constructor ();
  getUserName(): string;
  setUserName(a: string): void;
  serializeBinary(): Uint8Array;
  static deserializeBinary: (bytes: {}) => GetUserRequest;
}

export class GetUserResponse {
  constructor ();
  getUser(): User;
  setUser(user: User): void;
  serializeBinary(): Uint8Array;
  static deserializeBinary: (bytes: {}) => GetUserResponse;
}

export class User{
  constructor ();
  getName(): string;
  setName(a: string): void;
  serializeBinary(): Uint8Array;
  static deserializeBinary: (bytes: {}) => User;
}

Looking at the generator's code, I see that only numbers and strings are being taken into account when printing class fields types. Other well known types like array or map aren't being considered either.

codegen typescript

Most helpful comment

@aberasarte Thanks for the report. Yes we can certainly do better in the .d.ts typings output. Will work on this soon.

All 9 comments

I'm experiencing the same issue after generating the ts files with protoc.

syntax = "proto3";

package books;

service BookService {
  rpc List (Empty) returns (BookList) {}
  rpc Insert (Book) returns (Empty) {}
  rpc Get (BookIdRequest) returns (Book) {}
  rpc Delete (BookIdRequest) returns (Empty) {}
  rpc Watch (Empty) returns (stream Book) {}
}

message Empty {}

message Book {
  int32 id = 1;
  string title = 2;
  string author = 3;
}

message BookList {
  repeated Book books = 1;
}

message BookIdRequest {
  int32 id = 1;
}

Can one generate this book.proto file for me in grpc-web js and ts!
I'm having some difficulties in compiling it on my windows.

@aberasarte Thanks for the report. Yes we can certainly do better in the .d.ts typings output. Will work on this soon.

@Generalomosco
The files.
Im using hyper v with ubuntu
book.zip

Just to add on. Can we have ClientReadableStream callback type to be any instead of Array<{}>? I am using the toObject method and it is giving type check errors.

  export class ClientReadableStream {
    on (type: string,
        callback: (...args: Array<{}>) => void): ClientReadableStream;
    cancel (): void;
  }

I feel that using any first will be a better move and it will be easier for us to transition once the typing files get better

This is also happening with well known types; when using the types from the example and replacing message_interval int32 with google.protobuf.Duration:

export class ServerStreamingEchoRequest {
  constructor ();
  getMessage(): string;
  setMessage(a: string): void;
  getMessageCount(): number;
  setMessageCount(a: number): void;
  getMessageInterval(): {};
  setMessageInterval(a: {}): void;
  serializeBinary(): Uint8Array;
  static deserializeBinary: (bytes: {}) => ServerStreamingEchoRequest;
}

Interestingly, maybe this is a space where collaboration with https://github.com/improbable-eng/ts-protoc-gen could pay off? Their generator already has much better support for TypeScript bindings, with no dependencies other than google-protobuf and any imported types.

Looks like it not fixed.

@stanley-cheung can you provide some ETA for that bug?

Looks like it should be supported FieldDescriptor::TYPE_MESSAGE for repeated. Missing entity TypeScript definition as issue author mentioned:

The getUser() method returns {} when it could be User. The export class User is missing as well.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

smnbbrv picture smnbbrv  路  4Comments

rwlincoln picture rwlincoln  路  6Comments

pumano picture pumano  路  6Comments

barrymichaeldoyle picture barrymichaeldoyle  路  4Comments

joaomlneto picture joaomlneto  路  4Comments