grpc-node: Cannot compile TypeScript hello world client/server

I was following the quickstart document to implement the hello world client server example in TypeScript.

I can compile and run the code fine if I have noImplicitAny set to false in tsconfig.json. However, if I have it set to true, I get the following error when running tsc -p .:

node_modules/grpc/index.d.ts:2:55 - error TS7016: Could not find a declaration file for module 'protobufjs'. '.../grpc-example/node_modules/protobufjs/dist/protobuf.js' implicitly has an 'any' type.
  Try `npm install @types/protobufjs` if it exists or add a new declaration (.d.ts) file containing `declare module 'protobufjs';`

2   import { Message, Service as ProtobufService } from "protobufjs";
                                                        ~~~~~~~~~~~~

Also, running npm install @types/protobufjs does not resolve the issue.

Environment

  • OS name, version and architecture: OS X Mojave
  • Node version: 8.9.1
  • Node installation method: nvm
  • Package name and version: @grpc/proto-loader@0.3.0, grpc@1.15.1

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 2
  • Comments: 32 (16 by maintainers)

Commits related to this issue

Most upvoted comments

hello. I’m able to compile the server but not the hello world client as I’m getting this error:

Property 'Greeter' does not exist on type 'ProtobufTypeDefinition | GrpcObject | ServiceClientConstructor'.
  Property 'Greeter' does not exist on type 'ProtobufTypeDefinition'.ts(2339)

Same problem here.

You explicitly added a dependency on @types/protobuf.js version 6.x. Since grpc depends on protobufjs version 5.x as mentioned previously in this issue, that is probably creating a compatibility problem.

Any progress here? Seems like most developers who would be using gRPC are also highly likely to be using Typescript as well

@murgatroid99 it appears the typescript definitions aren’t available in the 5.x release of protobufjs, which this depends on. The typescript definitions are available in the 6.x releases.

The proto-loader package is set to a newer version already:

https://github.com/grpc/grpc-node/blob/073dc563858a2d74bd114e5cc50d02203123078f/packages/proto-loader/package.json#L41-L44

But the grpc-native-core is still pegged to an old version:

https://github.com/grpc/grpc-node/blob/10a0472f1e205833a86104a79405d4722ad6af35/packages/grpc-native-core/package.json#L31-L37

Oddly, this comment and object lead me to believe the module was already developed with v6 but that package.json just wasn’t updated:

https://github.com/grpc/grpc-node/blob/04395a0bc88a64aa67f1c299ad6d1504b6af32bc/packages/grpc-native-core/index.d.ts#L31-L74

Based on in person feedback from @kjin, I installed protobufjs. This solved the issue since protobufjs includes type definitions.

Note that I still had the errors below. However, this is expected since, IIUC, the types from the proto file are not automatically annotated on the helloProto object created. Thus it is necessary to explicitly cast it to the correct type to instruct TypeScript of its type.

rc/client.ts:19:33 - error TS2339: Property 'Greeter' does not exist on type 'Message | GrpcObject | typeof Client'.
  Property 'Greeter' does not exist on type 'Message'.

19   const client = new helloProto.Greeter('localhost:50051',
                                   ~~~~~~~


src/server.ts:26:32 - error TS2339: Property 'Greeter' does not exist on type 'Message | GrpcObject | typeof Client'.
  Property 'Greeter' does not exist on type 'Message'.

26   server.addService(helloProto.Greeter.service, {
                                  ~~~~~~~