Discriminated union doesn't work with `transform`
See original GitHub issuePossibly 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:
- Created a year ago
- Reactions:3
- Comments:6 (1 by maintainers)
Top 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 >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
I made a pull request to address this issue as I have the same problem.
Waiting for merge or feedback
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?