Skip to content

Zod resolver infered types are incorect since 4.1.0 #743

Closed
@vanwalj

Description

@vanwalj

Describe the bug
zodResolver now infer schema type which is great but unfortunately produces invalid typings

To Reproduce
Setup a project using zod, typescript and react-hook-form, create a simple form such as

const FORM_SCHEMA = z.object({
  s: z.string(),
});

const App = () => {
  const form = useForm({
    resolver: zodResolver(FORM_SCHEMA),
  });

  const [s] = form.watch(["s"]);

  console.log(s);
  ...
};

Codesandbox link (Required)
https://codesandbox.io/p/sandbox/wispy-wildflower-fk7ymp?file=%2Fsrc%2Findex.js%3A11%2C4

Expected behavior
s is typed as string but should be typed as string | undefined

Screenshots
Image

Desktop (please complete the following information):

  • OS: macOS 15.3
  • Browser any
  • Versions: zod 3.24.2, react-hook-form 7.54.2, @hookform/resolvers 4.1.0

Additional context

I personally used to provide useForm both TFieldValues and TTransformedValues typings so that everything is properly typed such as

const form = useForm<
    DeepPartial<z.input<typeof SCHEMA>>,
    unknown,
    z.output<typeof SCHEMA>
  >({
    resolver: zodResolver(SCHEMA),
  });

but even this is now impossible since types are clashing: Types of parameters 'values' and 'values' are incompatible

Thank you 🙇

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions