Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge not supported by union types #147

Closed
josejulio opened this issue Sep 11, 2020 · 8 comments
Closed

Merge not supported by union types #147

josejulio opened this issue Sep 11, 2020 · 8 comments

Comments

@josejulio
Copy link

josejulio commented Sep 11, 2020

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

@colinhacks
Copy link
Owner

That's right, you'd have to do this at the top level:

const IntersectionType = z.union([ 
  Type2.merge(Type1Sub1), 
  Type2.merge(Type1Sub2) 
]);

It's essentially the same in terms of lines-of-code and way more readable/understandable. It also eliminates a lot of internal parsing complexity associated with intersections.

@josejulio
Copy link
Author

One question though, is z.intersection going away? or it is just deprecated for merging two objects together? I'm asking because of primitive types. I haven't seen other ways of doing that.

I'm building a small openapi to zod tool. The ability to chain the calls together is really useful for spitting the code while reading the openapi json.

@colinhacks
Copy link
Owner

I'm asking because of primitive types. I haven't seen other ways of doing that.

Can you elaborate on this?

@josejulio
Copy link
Author

Something like this:

const tagsA = z.enum([ 'a', 'b', 'c' ]); // type tagsA = 'a' | 'b' | 'c'
const tagsB = z.enum([ 'b', 'c', 'd' ]); // type tagsB = 'b' | 'c' | 'd'

const bOrC = z.intersection(tagsA, tagsB); // ('a' | 'b' | 'c') & ('b' | 'c' | 'd') 
type bOrC = z.infer<typeof bOrC>; // 'b' | 'c'

@colinhacks
Copy link
Owner

colinhacks commented Sep 17, 2020

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

@josejulio
Copy link
Author

Thank you 😁

@colinhacks
Copy link
Owner

You bet :)

@david8z
Copy link

david8z commented Dec 17, 2024

In case someon wants a more low level solution rather than intersection:

export const mergeDiscriminatedUnion = <
	Discriminator extends string,
	Options extends z.ZodDiscriminatedUnionOption<Discriminator>[],
	Shape extends z.ZodRawShape,
>(
	discriminatedUnion: z.ZodDiscriminatedUnion<Discriminator, Options>,
	objectToMerge: z.ZodObject<Shape>,
) => {
	return z.discriminatedUnion(
		discriminatedUnion._def.discriminator,
		// @ts-expect-error
		discriminatedUnion._def.options.map((option) =>
			option.merge(objectToMerge),
		),
	) as z.ZodDiscriminatedUnion<
		Discriminator,
		[
			...{
				[K in keyof Options]: Options[K] extends z.ZodTypeAny
					? z.ZodObject<z.objectUtil.extendShape<Options[K]["shape"], Shape>>
					: never;
			},
		]
	>;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants