Allow more types in discriminated unions
See original GitHub issueIt 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.
Issue Analytics
- State:
- Created a year ago
- Reactions:12
- Comments:11 (2 by maintainers)
Top Results From Across the Web
Demystifying TypeScript Discriminated Unions - CSS-Tricks
TypeScript discriminated unions are types where a key or value can be used to differentiate between the keys and values of several types...
Read more >Handbook - Unions and Intersection Types - TypeScript
Discriminating Unions. A common technique for working with unions is to have a single field which uses literal types which you can use...
Read more >Discriminated Unions - TypeScript Deep Dive - Gitbook
If you have a class with a literal member then you can use that property to discriminate between union members. ... type Shape...
Read more >TypeScript discriminated union and intersection types
Union types produce a new type that allows us to create objects with some or all of the properties of each of the...
Read more >The case for Discriminated Union Types with Typescript
How might we avoid this mismatch between the state of the problem and our domain modeling? Typescript allows us to combine different types...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@dbeckwith, @scotttrinh, check out https://github.com/colinhacks/zod/pull/1213, which at least partially solves these issues.
Another one is
intersection
: