Possible to extend or merge across a union?
See original GitHub issueIs it possible to extend or merge across a zod union?
The use case I have here is that I have a polymorphic type which I store in a JSON field in my DB where I want to: (a) represent the input I get which does not have an id on it (b) represent what I get from the database, which now has an id on it
I’ve provided an example below - I tried a few other attempts without any luck. Please let me know if there’s a way to do this - thank you!
// example:
const TestThing1 = z.object({
type: z.literal('thing1'),
// ... other properties not shared
});
const TestThing2 = z.object({
type: z.literal('thing2'),
// ... other properties not shared
});
const TestThing3 = z.object({
type: z.literal('thing3'),
// ... other properties not shared
});
const ThingInput = z.union([TestThing1, TestThing2, TestThing3]);
const BaseThing = z.object({
id: z.string().uuid(),
});
// I want to do something like this, but I get an error (see below)
const Thing = z.union([TestThing1, TestThing2, TestThing3].map(BaseThing.merge));
// Error
Argument of type 'ZodObject<extendShape<{ id: ZodString; }, { type: ZodLiteral<"thing1">; } | { type: ZodLiteral<"thing2">; } | { type: ZodLiteral<"thing3">; }>, "strip", ZodTypeAny, { ...; }, { ...; }>[]' is not assignable to parameter of type '[ZodTypeAny, ZodTypeAny, ...ZodTypeAny[]]'.
Source provides no match for required element at position 0 in target.ts(2345)
Issue Analytics
- State:
- Created 2 years ago
- Comments:5
Top Results From Across the Web
Aggregate, Join, or Union Data - Tableau Help
Aggregate, join, or union your data to group or combine data for analysis. ... is a table that's typically extended horizontally by adding...
Read more >Merge and unmerge cells - Microsoft Support
How to merge and unmerge cells (text or data) in Excel from two or more ... After merging cells, you can split a...
Read more >Unions and interfaces - Apollo GraphQL Docs
Unions and interfaces are abstract GraphQL types that enable a schema field to return one of multiple object types. Union type.
Read more >Merger: Definition, How It Works With Types and Examples
A merger is an agreement that unites two existing companies into one new company. Learn here why it happens and the different types...
Read more >Documentation - Declaration Merging - TypeScript
This merged definition has the features of both of the original declarations. Any number of declarations can be merged; it's not limited to...
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
To be clear Typescript supports this and it’s awkward in Zod to do this because you have to nest under another key unless there’s a workaround I’m not aware of.
Gotcha, yeah I had a feeling I would need to do something like that. Thanks for taking a look!