✨ Extract a Type from a Parser Object ✨
We have a function to print our StarWarsPeopleResults to the console:
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.
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:
- Medium: https://medium.com/@nhannguyendevjs/
- Dev: https://dev.to/nhannguyendevjs/
- Hashnode: https://nhannguyen.hashnode.dev/
- Linkedin: https://www.linkedin.com/in/nhannguyendevjs/
- X (formerly Twitter): https://twitter.com/nhannguyendevjs/
Top comments (0)