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.

Discriminated union doesn't work with `transform`

See original GitHub issue

Possibly it is not bug. Any way to “post”-define discriminator (define in transform)?

Codesandbox example 1 Codesandbox example 2

Example 1:

import z from "zod";

const schemaA = z
  .object({
    mo: z.number().optional(),
    aa: z.number()
  })
  .transform((content) => ({
    ...content,
    k: "kindA" as const
  }));

const schemaB = z
  .object({
    mi: z.number().optional(),
    aao: z.number()
  })
  .transform((content) => ({
    ...content,
    k: "kindB" as const
  }));

const schemaC = z.discriminatedUnion("k", [schemaA, schemaB]);

Example 2:

import z from "zod";

const schemaA = z.object({
  T: z.literal("error"),
  c: z.number(),
  m: z.string()
});

const schemaB = z
  .object({
    T: z.literal("ss"),
    mi: z.number().optional(),
    aao: z.number()
  })
  .transform((content) => ({
    ...content,
    someField: "someValue" as const
  }));

const schemaC = z.discriminatedUnion("T", [schemaA, schemaB]);

Error: The discriminator value could not be extracted from all the provided schemas

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:3
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
roblabatcommented, Jul 26, 2022

I made a pull request to address this issue as I have the same problem.

Waiting for merge or feedback

4reactions
davidtorosyancommented, Dec 12, 2022

I see the fix is now available in v3.20.0-beta, but when I upgraded and tried the first example I got this error:

Type 'ZodEffects<ZodObject<{ mo: ZodOptional<ZodNumber>; aa: ZodNumber; }, "strip", ZodTypeAny, { mo?: number | undefined; aa: number; }, { mo?: number | undefined; aa: number; }>, { ...; }, { ...; }>' is missing the following properties from type 'ZodObject<{ k: ZodTypeAny; } & ZodRawShape, any, any, { [x: string]: any; }, { [x: string]: any; }>': _cached, _getCached, shape, strict, and 14 more.ts(2740)

Am I doing something wrong?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why discriminated union typescript doesn't work?
You've split the object apart into independant local variables, so there's no longer a link between the types.
Read more >
Impossible to transform from a discriminated union to another ...
It does not seem to be possible to change the discriminant from one property to another and have that recognised as the proper...
Read more >
Discriminated Unions - TypeScript Deep Dive - Gitbook
Discriminated Union ​​ If you have a class with a literal member then you can use that property to discriminate between union members....
Read more >
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 >
The case for Discriminated Union Types with Typescript
It is quite common when modeling a real-life problem to have constraints such as having a user account with either a nickname or...
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