Skip to content

Zod 4.0.15 and Superforms incompatibilityΒ #630

@brandonpittman

Description

@brandonpittman
  • Before posting an issue, read the FAQ and search the previous issues.

Description
A clear and concise description of what the bug is, and, unless obvious, what you expected instead.

The following use of Superforms and a simple schema presents a TypeScript error that didn't present on Zod 3.

export const load = async (event) => {
	const { locals } = event;
	const user = locals.user;
	const form = await superValidate({ value: user.user_info.gender }, zod4(GenderSchema));

	if (user.consented_at == null) {
		return error(404, 'Not found.');
	}

	return {
		form,
		title: m['gender-title']()
	};
};

export const actions = {
	default: async (event) => {
		const { request, cookies, locals } = event;
		const form = await superValidate(request, zod4(GenderSchema));

		if (!form.valid) {
			return fail(400, { form });
		}
		const user = locals.user;
		user.user_info.gender = form.data.value as Gender;

		setUserCookie(cookies, user);

		throw proceed();
	}
};
import { object, string } from 'zod/mini';

export const GenderSchema = object({
	value: string()
});

The error in the editor is this:

Argument of type 'ZodObject<{ value: ZodString; }, $strip>' is not assignable to parameter of type 'ZodValidationSchema'.
  Type 'ZodObject<{ value: ZodString; }, $strip>' is not assignable to type '$ZodDiscriminatedUnion<($ZodObject<Readonly<Readonly<{ [k: string]: $ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>>; }>>, $ZodObjectConfig> | $ZodDiscriminatedUnion<...>)[]>'.
    The types of '_zod.def' are incompatible between these types.
      Type '$ZodObjectDef<{ value: ZodString; }>' is missing the following properties from type '$ZodDiscriminatedUnionDef<($ZodObject<Readonly<Readonly<{ [k: string]: $ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>>; }>>, $ZodObjectConfig> | $ZodDiscriminatedUnion<...>)[]>': discriminator, options (tsserver 2345)

The validation is working but the types are wrong.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions