grpc-node icon indicating copy to clipboard operation
grpc-node copied to clipboard

proto-loader-gen-types generate nullable/undefinable outputs

Open LeeStephen opened this issue 2 years ago • 2 comments

Is your feature request related to a problem? Please describe.

In Typescript 4.4, the compiler option "exactOptionalPropertyTypes" was introduced. This feature distinguishes between the use of "?" and explicitly-defined "undefined".

The types generated from 'proto-loader-gen-types' generates with "?" for the output by default. When using the types for client code input, we cannot easily write conditional inclusion/exclusion of fields if "exactOptionalPropertyTypes" is set.

Any code structured like the following would no longer work: { myProperty: myAssignedValue || undefined }

Instead, it would need to be refactored to something akin to this: { ...(myAssignedValue ? { myProperty: myAssignedValue } : {}) }

Describe the solution you'd like

proto-loader-gen-types should generate with explicit "undefined" alongside the "?". Also, it would be nice if "null" was also supported, as grpc-js does seem to respect null from client code (Seems to effectively map to "undefined" under the hood).

Describe alternatives you've considered

I've tried importing the type and generating the argument prior to the client call, but that ends up with me writing a lot of extra code just to manage explicit undefined. For codebases that have already been written with explicit undefined, it would take a lot of effort to turn on "exactOptionalPropertyTypes".

LeeStephen avatar Feb 25 '22 00:02 LeeStephen

This seems like a reasonable change. If you make a pull request I will accept it, otherwise it may take me a while to get to it myself.

murgatroid99 avatar Mar 03 '22 23:03 murgatroid99

I would really like the null change as well, as Protobuf.js types contain null and you can't easily pass them to gRPC service methods because of this.

pikaju avatar May 14 '22 08:05 pikaju