ts-protoc-gen icon indicating copy to clipboard operation
ts-protoc-gen copied to clipboard

Question: Use of Object Literals

Open wtatum opened this issue 5 years ago • 9 comments

The generated types for messages are classes with a no-args constructor, and settings/getters for each property. In many cases (especially unary request/response operations) this feels really superfluous and adds a lot of cruft to code that could otherwise be inlined. As a new user of GRPC it's not clear to me whether this is an artifact of ts-protoc-gen or it's just representative of the underlying types created in JS.

This can be partially addressed using toObject() but that's only available one way. Am I missing something meant to make this easier?

wtatum avatar Jun 15 '20 18:06 wtatum

I found someone asking basically the same question over on the grpc-node project, where they were told this behavior is determined by the protobuf project, not by any of the protoc plugins. https://github.com/grpc/grpc-node/issues/1378

Does that sound right?

wtatum avatar Jun 15 '20 20:06 wtatum

Cross posted this to Google Groups forum for Protobuf

https://groups.google.com/forum/#!forum/protobuf

wtatum avatar Jun 15 '20 20:06 wtatum

I have the same issue. Seems like this project is being forked in some cases and isn't currently active? Are PRs accepted?

grant avatar Jul 07 '20 06:07 grant

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Oct 12 '20 01:10 stale[bot]

It seems that the tutorial uses the "optimal" JS api but if you compile the proto files, then you need to use the getters/setters:

E.g. see https://github.com/grpc/grpc/tree/v1.33.1/examples/node/dynamic_codegen (using normal objects) versus https://github.com/grpc/grpc/tree/v1.33.1/examples/node/static_codegen (using getters / setters)

entropitor avatar Oct 28 '20 19:10 entropitor

If you want to use the former, you can use https://github.com/grpc/grpc-node/pull/1474 to get TS definitions

entropitor avatar Oct 29 '20 07:10 entropitor

@entropitor i generated the TS definition with grpc/grpc-node#1474 . Could you explain, how you use/incorporate the ts definitions with the generated ts code from ts-protoc-gen?

RobinKamps avatar Nov 18 '20 17:11 RobinKamps

You can follow the examples on the grpc website. But when using typescript you can e.g. type your functions/services using one of the types that is generated (just add a manually type annotation)

entropitor avatar Nov 18 '20 18:11 entropitor

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 17 '22 07:04 stale[bot]