blokur / grpc-ts-demo

šŸŽµ Demo of a gRPC client and server implementation in TypeScript
MIT License
123 stars 30 forks source link

@grpc/grpc-js Server addService(..) function and @ts-ignore #24

Open ghost opened 1 year ago

ghost commented 1 year ago

Your demo is fantastic.

The tldr; is - it helped me resolve two issues I couldn't figure out:

  1. grpc.Server.addService(..) would not transpile no matter what I gave it in my typescript app - so I ignored that statement as you did
  2. no matter what arguments I gave to "protoc" - it failed to generate the correct output (exp: it would reference the "window" global from browsers) - so I switch to grpc_tools_node_protoc as you did

grpc.Server.addService(..)

The following is just a more thorough account of my findings and why I wasn't able to resolve my issues without just doing what you did.

Something I've been banging my head against the wall is the Server addService(..) function - i.e. this:

grpc.Server.addService(service: ServiceDefinition, implementation: UntypedServiceImplementation)

In your code - you did the following:

    // @ts-ignore
    server.addService(SongsService, new SongsServer());

Without the ts-ignore - tsc doesn't succeed in the transpile and I can't understand why.

I've found number reports/issues related to this bit of code - but up until now I haven't been able to find a way to work-around the issue. The most relevant post I found was this: https://github.com/grpc/grpc-node/issues/1766

But, that issue was reported over a year ago and apparently was fixed in grpc version 1.24.11.

I found some other issues that are related to this - exp: https://github.com/agreatfool/grpc_tools_node_protoc_ts/issues/79

But, again these posts are over a year old - so I must be missing something else.

Anyway, your sample/tutorial helped me overcome that issue - i.e. disabling the typescript check on that statement.

protoc vs grpc_tools_node_protoc

No matter what arguments I gave to protoc - it

protoc \
    --plugin="protoc-gen-ts=${PROTOC_GEN_TS_PATH}" \
    --js_out="import_style=commonjs,binary:${TS_OUT_DIR}" \
    --ts_out="service=grpc-node,mode=grpc-js:${TS_OUT_DIR}" \
    -I="./proto" \
    ./proto/com/generic/v1/services/*.proto

the generated code would create a statement like the following in the code:

var global = (function() { return this || window || global || self || Function('return this')(); }).call(null);

You can read about this issue here: https://github.com/protocolbuffers/protobuf-javascript/issues/8

I tried using a variety of different versions of various tools, plugins, etc to no avail.

reececomo commented 1 month ago

Without the ts-ignore - tsc doesn't succeed in the transpile and I can't understand why.

šŸ‘ same issue