DEV Community

Nhan Nguyen
Nhan Nguyen

Posted on

Zod - TypeScript-first schema declaration and validation library #5

Image description

Extract a Type from a Parser Object

We have a function to print our StarWarsPeopleResults to the console:

Image description

Update the Logging Function:

Using z.infer and passing it typeof StarWarsPeopleResults.

This passes in the schema from Zod, and returns a type that properly represents the data.

Image description

Now, when you hover over the line in VS Code, you can see that data is an object that contains the results.

Making updates to the StarWarsPerson schema will update the data inside the function right away.

This is a great way to use Zod for runtime validation and then getting our type validation from it.


I hope you found it useful. Thanks for reading. 🙏
Let's get connected! You can find me on:

Top comments (0)