Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating regular typescript interfaces representing services in ts-proto #115

Closed
SoftMemes opened this issue Apr 6, 2022 · 4 comments · Fixed by #126
Closed

Generating regular typescript interfaces representing services in ts-proto #115

SoftMemes opened this issue Apr 6, 2022 · 4 comments · Fixed by #126

Comments

@SoftMemes
Copy link

I have raised this as an issue / feature request on ts-proto as the actual change would be needed there, but would be interested to get the feedback from this end on the proposal, see stephenh/ts-proto#545

In short, I do quite like the model of using a single definition for a service that can be used for service implementations as well as clients, though the Typescript magic required for this on the service side does mean that IDE tooling and error messages are not quite as helpful as they could be.

If ts-proto could generate an actual regular typescript interface that a service would implement, with regular method signatures, then IDE tooling such as "implement interface" would presumably give more readable signatures, and type errors may be somewhat more obvious than what they are now.

Is there anything in principle that would prevent this from working, or being a good idea?

@aikoven
Copy link
Contributor

aikoven commented Apr 7, 2022

I agree that it would be really nice to have.

One concern is — if we decide to make a breaking change in nice-grpc service implementation signatures (or client signatures) — we would have to make a breaking change in ts-proto as well, or at least have different plugin options for nice-grpc v1 and v2. On the other hand, I don't think that it would happen soon (or at all). Apart from the signatures, the ServiceImplementation and Client types only use types imported from nice-grpc-common, which is designed to be very stable. So even if breaking changes in nice-grpc affect other parts of it — the generated interfaces should continue to work.

Another concern is the fromPartial method. Right now, nice-grpc automatically wraps responses on the server and requests on the client with fromPartial. So the typings expect service implementation methods to return the DeepPartial<Response>. This is not a problem on its own, but we had related problems in the past, for example stephenh/ts-proto#454. I would also like to be able to use shallow partial instead of deep partial in the future, see stephenh/ts-proto#378.

So there are no blockers to your proposal, but there are some things to keep in mind.

@aikoven
Copy link
Contributor

aikoven commented Apr 24, 2022

Opened a PR: stephenh/ts-proto#555

@aikoven
Copy link
Contributor

aikoven commented May 2, 2022

Starting from version 1.112.0 ts-proto is able to generate TypeScript interfaces for nice-grpc service implementations and clients. So instead of ServiceImplementation<typeof YourServiceDefinition> you can write YourServiceImplementation and instead of Client<typeof YourServiceDefinition> just use YourServiceClient. This will also make IDE hints more developer-friendly.

To start using this feature, add outputServices=nice-grpc to ts_proto_opt. Note that outputServices=generic-definitions is still required, i.e. you should include outputServices option twice. For complete examples see the docs on compiling Protobuf files for nice-grpc and nice-grpc-web.

@SoftMemes
Copy link
Author

This is a great quality of life improvement, much appreciated, and many thanks for taking the feedback onboard so quickly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants