Form Validation With Type Inference Made Easy With Zod, The Best Sidekick For Typescript

Form Validation With Type Inference Made Easy With Zod, The Best Sidekick For Typescript

Zod is amazing! You can use it to define the schema of any object and validate it. Form data? No problem. Nested arrays and objects? We've got you covered. Custom data types? Yeah!

And if you're new to TypeScript, Zod can help you define types in a more intuitive way: define the object schema using Zod functions, then use z.infer to infer the types.

const taskSchema = z.object({
    title: z.string(),
    points: z.number(),
    completed: z.boolean(),
    comment: z.string().optional()
})

type Task = z.infer<typeof taskSchema>

Read all about it in my article on Tighten's blog. Click the box below:

And yes, the library gets its name from the Superman villain. Great!