Merge not supported by union types
See original GitHub issueI 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:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top 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 >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
Thank you 😁
Okay this seems like a valid use case. I’ll leave z.intersection alone for now 😛 I removed the deprecation notice.