question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

missing generics in generated typescript file

See original GitHub issue

Hi there,

first of all, I am working on a windows machine. I got the latest precompiled version of the protoc for windows found on https://github.com/protocolbuffers/protobuf/releases/tag/v3.6.1

I’ve checked out the grpc-web version https://github.com/grpc/grpc-web/commit/e03f5ddb63cc872224167897f20af71047b6cb8e and compiled the protoc-gen-grpc-web plugin with bazel myself.

My proto file looks like ths:

service TestProto {
    rpc getTest(TestRequest) returns (TestReply) {}
    rpc getTestStream(TestRequest) returns (stream TestReply) {}
}

message TestRequest {
    int32 id = 1;
}

message TestReply {
    string reply = 1;
}

I am using the npm packages “grpc-web”: “1.0.2” and “google-protobuf”: “3.6.1” Now I compile the proto file with :

protoc --proto_path="$protoFilePath/" --js_out="import_style=commonjs:$CurrentOutDir" --grpc-web_out="import_style=commonjs+dts,mode=grpcwebtext:$CurrentOutDir" $protoFile

The following methods will be generated:

getTest(
    request: TestRequest,
    metadata: grpcWeb.Metadata,
    callback: (err: grpcWeb.Error,
      response: TestReply) => void
  ): grpcWeb.ClientReadableStream;

  getTestStream(
    request: TestRequest,
    metadata: grpcWeb.Metadata
  ): grpcWeb.ClientReadableStream;

Which gets me a compiler error Generic type ‘ClientReadableStream<Response>’ requires 1 type argument(s). Manually correcting this to grpcWeb.ClientReadableStream<TestReply> solves the issue and renders the code functional again.

There is similar behavior when setting the output to--grpc-web_out="import_style=typescript Which generates the following method:

getTestStream(
    request: TestRequest,
    metadata: grpcWeb.Metadata) {
    return this.client_.serverStreaming(
      this.hostname_ +
        '/TestProto/getTestStream',
      request,
      metadata,
      this.methodInfogetTestStream);
  }

This gets me this compiler error Argument of type ‘TestRequest’ is not assignable to parameter of type ‘Request’. Property ‘cache’ is missing in type ‘TestRequest’

In order to make this work again I had to update index.d.ts in the grpc-web npm package which exports the following:

export class AbstractClientBase {
rpcCall<Request, Response> [...]
serverStreaming [...]

since serverStreaming doesn’t have any generics defined, a wrong type for Request is assumed. Adding the generic to the serverStreaming method makes everything work again.

export class AbstractClientBase {
[...]
serverStreaming<Request, Response> [...]

I guess this is a bug, or is there anything that I did wrong?

Thanks in advance!

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
yinhmcommented, Jan 28, 2019

I have encountered this as well. protoc --version libprotoc 3.6.1 protoc-gen-grpc-web 1.0.3 grpc-web: ^1.0.3

Error message from tsc

ERROR in proto/ApiServiceClientPb.ts(116,7): error TS2345: Argument of type 'Sub' is not assignable to parameter of type 'Request'.
  Property 'cache' is missing in type 'Sub'.
proto/ApiServiceClientPb.ts(289,7): error TS2345: Argument of type 'Range' is not assignable to parameter of type 'Request'.
  Property 'cache' is missing in type 'Range'.
0reactions
iiiancommented, Jan 18, 2022

nevermind, was able to resolve my issue by opting for --grpc-web_out=import_style=commonjs+dts instead of --grpc-web_out=import_style=typescript

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix 'Type is missing properties from type' with generic ...
Consider a library which exports a run function like below: runner.ts
Read more >
Documentation - Generics - TypeScript
In the next section, we'll cover how you can create your own generic types like Array<Type> . Generic Types. In previous sections, we...
Read more >
Not recognizing TypeScript generics in JavaScript : WEB-42617
It looks like WebStorm is having troubles recognizing generic types that are defined in a TypeScript definition file when they are used in...
Read more >
Typescript Generics Explained - Ross Bulat
The implementation of generics in Typescript give us the ability to ... but in the process we are losing the ability to define...
Read more >
Typescript Typings: The Complete Guide: @types Compiler ...
What are the multiple types of Typescript Type Definitions? ... But let's keep the file system clean of generated files, let's instead using ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found