Zod is a Typescript package that lets you declare your data types and do runtime type checking.
Lots of possible utilities for this library. Validation and type guards come to mind first, but Zod also includes a lot of helpful methods such as default
and transform
.
Quick example
typescript
import { z } from 'zod'
const Post = z.object({ id: z.string().uuid(), title: z.string().min(1), body: z.string()})
// Declare the typetype Post = z.infer<typeof Post>
const myPost = { id: 'c2cffa40-8169-4e9b-bce4-af21bc17b033', title: 'Validate schemas and declare types with Zod', body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'}
Post.safeParse(myPost)
// {// success: true,// data: {// id: 'c2cffa40-8169-4e9b-bce4-af21bc17b033',// title: 'Validate schemas and declare types with Zod',// body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'// }// }