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

google.protobuf.BoolValue fails typescript compiler when used as RPC response

Open jrkt opened this issue 2 years ago • 3 comments

We have methods like this:

rpc DoSomething(SomeRequest) returns (google.protobuf.BoolValue) { 

And the generated typescript gets generated without using the BoolValue wrapper message:

  DoSomething(request: SomeRequest): Promise<boolean | undefined> {
    const data = SomeRequest.encode(request).finish();
    const promise = this.rpc.request("somepackage", "DoSomething", data);
    return promise.then((data) => boolean | undefined.decode(new _m0.Reader(data)));
  }

as opposed to:

  DoSomething(request: SomeRequest): Promise<BoolValue> {
    const data = SomeRequest.encode(request).finish();
    const promise = this.rpc.request("somepackage", "DoSomething", data);
    return promise.then((data) => BoolValue.decode(new _m0.Reader(data)));
  }

We have other endpoints that use google/protobuf/empty.proto that do, in fact, use the Empty message. But, the BoolValue wrapper message isn't used. Is there any reason this is getting converted to a simple bool rather than using the BoolValue wrapper message?

This is causing typescript errors:

error TS2322: Type 'Promise<number | boolean | undefined>' is not assignable to type 'Promise<boolean | undefined>'.
  Type 'number | boolean | undefined' is not assignable to type 'boolean | undefined'.
    Type 'number' is not assignable to type 'boolean | undefined'.

10520     return promise.then((data) => boolean | undefined.decode(new _m0.Reader(data)));

jrkt avatar Sep 26 '22 15:09 jrkt

@stephenh is there a special option I need to provide or is this a bug?

jrkt avatar Sep 29 '22 15:09 jrkt

@stephenh it seems to be that using these wrapper types works when they are embedded in a message, but fail the typescript compiler when they are used as the RPC response.

jrkt avatar Oct 06 '22 20:10 jrkt

@boukeversteegh it seems like you've done work with the wrapper types. Do you have any ideas here?

jrkt avatar Oct 06 '22 20:10 jrkt

Hey @jrkt , obviously I'm super-late here replying / triaging issues, but yeah I think you're right that returning the boolean from methods is not working out.

I couldn't find it off-hand, but I believe another issue / one of the authors of that approach realized the same in a recent issue...ah yeah, it's this one:

https://github.com/stephenh/ts-proto/pull/689/files

I just merged that; obviously this is like 2 months after you've probably moved on, but if you want to try it out, lmk if that works for you. Going to optimistically assume that PR fixes your issue and close this out, but again lmk if that's not the case.

Thanks!

stephenh avatar Nov 13 '22 15:11 stephenh