DEV Community

Nhan Nguyen
Nhan Nguyen

Posted on

4

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:

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay