-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Q: why discriminatedUnion of one element is not possible #1442
Comments
Logical aspect is that the discriminator field in discriminated union with one variant carries no information and thus is obsolete?. The implementation does not necessitate having >1 variants, but the authors decided add that typelevel restriction for that reason likely. I see how in your generic code this is a problem since your code statically does allow such thing to happen - but then again zod is more of a static model definition library where you write most types plainly, so that all the const type magic works, and don't generate parsers like you do. You can though, as you said, hack it, I think the first part of my message answered your question of why it is like it is. |
Thank you for your answer. I see the reason now, but I can't help to wonder how the logical aspect of such a decision differs from three similar use cases handled in programming languages, including TS, completely differently: First example: Second example: Third example: Regarding const type magic, it works fine with my definitions as well But note that for the purpose of the question I simplified the code a little bit. I'll copy the complete code I have to clarify any confusion.
With all being said, now I'm even more confident that the requirement of a "minimum two elements" is an honest mistake. it should be a |
Hey @Firfi since I've been working on First, I agree with @necauqua in the math definition of the discriminated union. But that's just math! Click to see my opinionIt seems that the literal definition of a discriminated union (at least in math) is a disjoint union, which commonly is described in terms of a family of sets of Anyways! We live in the real world, and as of #1290 this works with one single entity as you desired. If this works for you feel free to close the issue and thanks! 🙏 |
Has this issue been resolved? If so I'd like to close this issue. |
@JacobWeisenburger : confirming it indeed works with the latest Zod. Thank you, @maxArturo for your work 👍 |
According to
discriminatedUnion
type definition, it accepts a list of at least two elements.My question is why is this implemented like this?
Question arose for the reason that I have a real use case where I have a factory function that would return parsers for n>=1 string literals i.e.
This function would generate parsers for such strings as
user::27f4ee03-e79e-470c-8d4e-fa28e8ac6089
but also a separate parsers for such strings asblog::7fe494ab-a349-4896-87f2-24e2024d2cce
orcollective::69e23e02-546b-4c30-a532-b3f45e8498f8
I could get by with if/else checking for my literals length, but it goes against normal intuition and would be akin to doing
I hope this is a clear enough parallel to why I see the definition of
discriminatedUnion
as a bit weird.There must be some technical restriction I don't see or some logical aspect I fail to recognize.
Could someone please help me to understand the reason for this implementation of
discriminatedUnion
?The text was updated successfully, but these errors were encountered: