Dev Overflow Logo

Dev Overflow

Global search

Search across questions, answers, users and tags.

Loading...
save

Best way to type a generic form component with react-hook-form and Zod?

clock icon

asked 1 month ago

message icon

1

eye icon

643

I want one <AuthForm /> that works for both sign-in and sign-up, driven by whichever Zod schema I hand it.

1interface Props<T extends FieldValues> {
2 schema: z.ZodType<T>
3 defaultValues: T
4}
1interface Props<T extends FieldValues> {
2 schema: z.ZodType<T>
3 defaultValues: T
4}

TypeScript then complains that Resolver<FieldValues, any, T> is not assignable to Resolver<T, any, T>. What is the correct way to express this?

1 Answer

Zod 4's ZodType<T> leaves the input side as unknown, while the resolver requires the input to extend FieldValues. Pin both sides:

1schema: z.ZodType<T, T>
1schema: z.ZodType<T, T>

With the input typed as T, standardSchemaResolver infers Resolver<T, unknown, T> and the assignment succeeds — no cast needed.

1

of 1

Write your answer here

Introduce the problem and expand on what you've put in the title.

Top Questions