React + TypeScript + React Hook-Form + Zod + vite
This project serves as a foundation for building modern, type-safe React applications with TypeScript and Zod. It integrates Vite for fast builds and development, providing tools for runtime schema validation and type inference with Zod.
Features React: Declarative and component-based UI library. TypeScript: Adds static typing to JavaScript for safer code. Zod: Provides runtime schema validation and automatic TypeScript inference. Vite: Lightning-fast build tool with Hot Module Replacement (HMR). Getting Started Installation Clone the repository:
bash Copy code git clone https://github.com/updalla-apshir/react-hook-form.git cd react-ts-zod-starter Install dependencies:
bash Copy code npm install
yarn install Start the development server:
bash Copy code npm run dev
yarn dev
Using Zod for Validation Why Zod? Zod provides an easy way to validate data structures at runtime, which is essential when dealing with data from external sources like APIs. It integrates well with TypeScript, offering automatic type inference from schemas.
Benefits of Zod:
Runtime validation for objects, arrays, etc. TypeScript type inference directly from schema definitions. Easy to use with minimal boilerplate.
Example Usage Defining a Schema typescript Copy code import { z } from "zod";
const userSchema = z.object({ name: z.string(), email: z.string().email(), age: z.number().int().nonnegative(), });
type User = z.infer; // Automatically inferred TypeScript type Validating Data typescript Copy code const userData = { name: "John Doe", email: "john.doe@example.com", age: 30, };
try { const user = userSchema.parse(userData); // Validates and returns the data console.log("Valid user:", user); } catch (error) { console.error("Validation error:", error.errors); }