protobuf-ts icon indicating copy to clipboard operation
protobuf-ts copied to clipboard

Protobuf Service (ServiceType) is generated as type any

Open alecmmoore opened this issue 3 years ago • 4 comments

Hi, there!

I am wondering if I have something misconfigured or if I have found a bug. I've put together a simple Hello World example for myself that I will eventually publish as an NPM package (hence the output_javascript_es2015.)

Version

// @generated by protobuf-ts 2.7.0 with parameter output_javascript_es2015,force_client_none,force_optimize_code_size
// @generated from protobuf file "proto/hello-world.proto" (package "helloWorldPackage", syntax proto3)

Current Result The generated files seem to be typed HelloWorldService as any.

Expected Result HelloWorldService is typed as a ServiceType

Setup

hello-world.proto

service HelloWorldService {
  rpc SayHello(SayHelloRequest) returns (SayHelloResponse) {
    option (google.api.http) = {
      post: "I Really Just Want To Get This String!",
      body: "*"
    };
  };
}

gen.sh

protoc -I=. ./proto/*.proto
  --ts_out=./dist \
  --ts_opt=output_javascript_es2015 \
  --ts_opt=force_client_none \
  --ts_opt=force_optimize_code_size \

Generated Files

hello-world.js

/**
 * @generated ServiceType for protobuf service helloWorldPackage.HelloWorldService
 */
export const HelloWorldService = new ServiceType("helloWorldPackage.HelloWorldService", [
    { name: "SayHello", options: { "google.api.http": { post: "I Really Just Want To Get This String!", body: "*" } }, I: SayHelloRequest, O: SayHelloResponse }
]);

hello-world.d.ts

/**
 * @generated ServiceType for protobuf service helloWorldPackage.HelloWorldService
 */
export declare const HelloWorldService: any;

Thank you so much for your time!

alecmmoore avatar Jul 18 '22 23:07 alecmmoore

const HelloWorldService: any sure looks odd, Alec. I would have expected a const HelloWorldService: ServiceType. Can you verify that this happens with other JavaScript targets as well?

timostamm avatar Jul 19 '22 07:07 timostamm

For sure! Looks like we get the same result:

// @generated by protobuf-ts 2.7.0 with parameter output_javascript_es2020,force_client_none,force_optimize_code_size
// @generated from protobuf file "proto/hello-world.proto" (package "helloWorldPackage", syntax proto3)
/**
 * @generated ServiceType for protobuf service helloWorldPackage.HelloWorldService
 */
export declare const HelloWorldService: any;

alecmmoore avatar Jul 19 '22 14:07 alecmmoore

Thank you for verifying. I think the best fix is to add an explicit typing to the variable declaration here:

https://github.com/timostamm/protobuf-ts/blob/02c82bdc66e1cf2bea199906c580e9d75ff93bcf/packages/plugin/src/code-gen/service-type-generator.ts#L56

Using ts.createTypeReferenceNode. See https://ts-ast-viewer.com/#code/MYewdgzgLgBAZiEAuGAhAhgJxgXhgRgCYBmIA

I don't have time to do this right now, but I'm happy to review a contribution.

timostamm avatar Jul 20 '22 07:07 timostamm

Thank you for looking into this!

alecmmoore avatar Jul 20 '22 15:07 alecmmoore