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.

Merge not supported by union types

See original GitHub issue

I saw that z.intersection is being deprecated in favor of merge, but the later does not yet support union types.

const Type1Sub1 = z.object({
    foo: z.string()
});

const Type1Sub2 = z.object({
    baz: z.string()
});

const Type1 = z.union([ Type1Sub1, Type1Sub2 ]);

const Type2 = z.object({
    foo: z.number(),
    baz: z.number()
});

const MergeType1 = Type1.merge(Type2); // does not work
const MergeType2 = Type2.merge(Type1); // does not work

const IntersectionType1 = z.intersection(Type1, Type2); // works: (Type1Sub1 & Type2) | (Type1Sub2 & Type2)
const IntersectionType2 = z.intersection(Type2, Type1); // works: (Type2 & Type1Sub1) | (Type2 & Type1Sub2)

edit: updated with objects, primitives don’t make much sense here

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
josejuliocommented, Sep 17, 2020

Thank you 😁

1reaction
colinhackscommented, Sep 17, 2020

Okay this seems like a valid use case. I’ll leave z.intersection alone for now 😛 I removed the deprecation notice.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Merge not supported by union types #147 - colinhacks/zod
I saw that z.intersection is being deprecated in favor of merge , but the later does not yet support union types.
Read more >
Merge discriminated union of object types in Typescript
That's not a discriminated union (a union where each member can be distinguished from the others by a common property with different literal ......
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 >
Union Type Merging in Typescript - DEV Community ‍ ‍
This guide describes merging an object union - another pattern that can be used to enhance type union usability.
Read more >
Handbook - Unions and Intersection Types - TypeScript
If we have a value that is a union type, we can only access members that are common to all types in the...
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