-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Closed
Labels
enhancementNew feature or requestNew feature or request
Description
It seems like z.discriminatedUnion currently has quite narrow requirements for what you can use it with. Here are some example use cases that seem sound to me (in the sense that the discriminator should still be able to uniquely determine the union member) but currently aren't allowed:
const A = z.object({ type: z.literal('a') });
const B = z.object({ type: z.literal('b') });
const C = z.object({ type: z.literal('c') });
const D = z.object({ type: z.literal('d').optional() });
const E = z.object({ type: z.enum(['e', 'ee', 'eee']) });
const AorB = z.discriminatedUnion('type', [A, B]);
// using another discriminated union with the same discriminator as a union member
const AorBorC = z.discriminatedUnion('type', [AorB, C]);
// the discriminator is optional in one union member
const CorD = z.discriminatedUnion('type', [C, D]);
// the discriminator is an enum
const CorE = z.discriminatedUnion('type', [C, E]);Any chance these cases can be supported? It would great if the requirement for discriminated union members could be widened to something like "any type that has the discriminator as a field and the type of the discriminator is a subtype of string | null | undefined and is mutually exclusive between the discriminated union members", but supporting at least the cases I mentioned above would still be much appreciated.
mtdewulf, Aaronius, fabyeah, ninesunsabiu, watsonsong and 13 more
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request