grpc / grpc/grpc-node

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

Open
#2,053 2 comments 3 reactions 0 assignees View on GitHub
package: @grpc/proto-loader
Dominant language
TypeScript
Stars
4.8k
Forks
716
Avg merge
2d 3h
Merged PRs (30d)
10

Description

### 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".

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.