-
Notifications
You must be signed in to change notification settings - Fork 347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support default instances on singular/required fields #1064
Comments
same request my hacky fix:
https://stackoverflow.com/questions/53050011/remove-null-or-undefined-from-properties-of-a-type |
@Simon-Chenzw I opened a PR #1066. Not sure if I covered everything. |
Hi @oliveryasuna , @Simon-Chenzw ts-proto's current behavior stems from two things: a) proto3's original "all message fields are always optional" behavior, and b) ts-proto's POJO-based approach. In proto clients that use Because ts-proto's messages are "just POJOs" (which is really the defining characteristic of this library, vs. other protobuf TypeScript libraries) and not classes, it's not clear how to implement this functionality. I.e. @oliveryasuna in your PR, this method:
Is creating So, to implement this functionality, you would at least need to teach ts-proto to create "default instances" of With that implemented, we would then need to decide if it's possible to retain the "hazzer" behavior, i.e. since we don't have methods-on-the-instance, we might have to do Further, the "default instance"-ness of This seems tricky to do. I guess we could generate an |
| undefined
to required properties
Fwiw I renamed the title; originally it had said "for required fields", but technically your example was using "singular fields" (where singular are "fields that aren't otherwise marked as optional or required or repeated"). I.e. a true proto required field would use the |
@stephenh There is clearly more to think about here than I thought. Here is my temporary workaround. I will just have another layer in between my proto/request and view model layers. interface FieldMetadata {
optional: boolean;
}
type PatchMessage<Message, Fields extends Record<Exclude<(keyof Message), '$type'>, FieldMetadata>> = {
[K in (keyof Message)]: (
K extends '$type'
? Message[K]
: (
Fields[K] extends FieldMetadata
? (Fields[K]['optional'] extends true
? (Message[K] | undefined)
: Exclude<Message[K], undefined>)
: Message[K]
)
);
}; |
@stephenh I think this is outside the scope of this project because it would break the pattern it implements. I will leave it to you to decide to close the issue/PR or not. |
I feel like this option should already exists, but I read through the README and could not find it. Please let me know if I am missing something.
Given a message:
It generates:
That is expected behavior.
However, if I have the following message:
It generates:
Why is
Test.inner
defined with| undefined
and why isTest.value
optional and also defined with| undefined
?The text was updated successfully, but these errors were encountered: