Recommendation for working with required fields? #363
Unanswered
raymondpebble
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey all, we're using
ts-proto
on the frontend to work with a GRPC API. Once we validate the proto returned from the backend, we'd like to work with types where the fields that are required by the client are notfieldType | undefined
.Some of the options we're considering are:
| undefined
for fields that are actually required. A little cumbersome but this would ensure we have type safety. Are there any techniques for making this easier to maintain?message?.field
and other type narrowing techniques wherever we use the proto types. This technically maintains type safety, but A) makes it hard to tell which fields are required and which ones are actually optional, since all fields "look" optional, and B) spreads the undefined checks everywhere.message!.field
locally after validating the object. This avoids 2.B) but puts the onus on users to make sure we're not making mistakes.Can anyone share their experience with any of these approaches or have others to suggest? Thanks so much!
Beta Was this translation helpful? Give feedback.
All reactions