Skip to content

issue: zodResolver disagrees with z.infer using transform APIs in Zod v4 #800

@Garrett-R

Description

@Garrett-R

Summary

We get disagreements in typing for at least the following transform APIs:

Version Number

5.2.0

Codesandbox/Expo snack

here

Steps to reproduce

Observe this code (also in Codesandbox ^):

import z from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm, UseFormRegister } from "react-hook-form";

const Foo = ({}) => {
  const MySchema = z.strictObject({
    numApples: z.number().nullable().default(null),
    numBananas: z.number().nullable(), // (no prob here)
  });
  type MySchemaType = z.infer<typeof MySchema>;

  const { register } = useForm({ resolver: zodResolver(MySchema) });
  const register2: UseFormRegister<MySchemaType> = register; // Type error!

  return <h1>Zod Form</h1>;
};

export default Foo;

Expected behaviour

No type errors.

But TS gives this error:

Type '{ numBananas: number | null; numApples?: number | null | undefined; }' is not assignable to type '{ numApples: number | null; numBananas: number | null; }'

I think z.infer is getting numApples type right (number | null) while zodResolver gets it wrong (number | null | undefined).

I'm on latest versions of react-hook-form, @hookform/resolvers, and zod.

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions