grpc-node
grpc-node copied to clipboard
proto-loader-gen-types generate nullable/undefinable outputs
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".
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.
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.