Interview frequency: …
Short answer
Define a Zod Schema, derive its TypeScript type with z.infer, and connect it to a React form with zodResolver for synchronous validation.
Explanation
Zod provides a declarative way to validate data on both the client and server. Together, zodResolver and React Hook Form automatically validate inputs, display matching messages, and provide accurate TypeScript autocomplete for form types without duplicating type definitions.
Code example
import { z } from 'zod';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
const loginSchema = z.object({
email: z.string().email("Invalid email"),
password: z.string().min(8, "Password must be at least 8 characters")
});
type LoginFormValues = z.infer<typeof loginSchema>;
function Form() {
const { register, handleSubmit, formState: { errors } } = useForm<LoginFormValues>({
resolver: zodResolver(loginSchema)
});
// Type-safe usage with automatic validation
}Common mistakes
- Writing a separate TypeScript interface that duplicates the Zod Schema.
Follow-up questions
- How do you build a Multi-step Wizard Form while preserving data when a user goes back?
- Why can React Hook Form (RHF) perform better than traditional Formik forms?