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.

Possible to extend or merge across a union?

See original GitHub issue

Is 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:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
jjm340commented, Feb 10, 2022

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.

1reaction
teddycolemancommented, Jan 14, 2022

Gotcha, yeah I had a feeling I would need to do something like that. Thanks for taking a look!

Read more comments on GitHub >

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

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