question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow more types in discriminated unions

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:12
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
tianhuilcommented, Jun 20, 2022

@dbeckwith, @scotttrinh, check out https://github.com/colinhacks/zod/pull/1213, which at least partially solves these issues.

3reactions
vlovichcommented, Apr 21, 2022

Another one is intersection:

z.discriminatedUnion('method', [
  z.intersection(
    z.object({ 'method': 'get' }),
    getSchema,
  ),
  z.intersection(
    z.object({ 'method': 'put' }),
    putSchema,
  ),
])
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found