resolvers icon indicating copy to clipboard operation
resolvers copied to clipboard

v5.0: Schema with .default fields result in inaccurate typing

Open eikowagenknecht opened this issue 7 months ago • 14 comments

Describe the bug With the new v5.0.0, I got a type error:

const FormSchema = z.object({
  debug_mode: z.boolean().default(true),
});
type FormValues = z.infer<typeof FormSchema>;
  const form = useForm<FormValues>({
    resolver: zodResolver(FormSchema),
    defaultValues: {
      debug_mode: true,
    },
  });

The automatic resolution gives me:

const form: UseFormReturn<{
    debug_mode?: boolean | undefined;
}

I think this is incorrect as the schema does not have any optional members.

When I remove the .default() in the schema, so it looks like this:

const FormSchema = z.object({
  debug_mode: z.boolean(),
});

the resolution changes to:

const form: UseFormReturn<{
    debug_mode: boolean;
}

which is consistent with type FormValues = z.infer<typeof FormSchema>; again.

Codesandbox link (Required)

As this is a typing issue and Code Sandbox uses JS, it's not really applicable.

eikowagenknecht avatar Apr 01 '25 10:04 eikowagenknecht