Parse and Infer Data Being Fetched in SvelteKit with Zod and JSDoc

InstructorBen Patton

Share this video with your friends

Send Tweet

Up to this point in our app, the only help we are getting from typescript is from SvelteKit's built in types. But these types don't infer our data structure so we need to add types using JSDoc and zod.

One of the benefits of zod is that it can be used for schema validation in javascript by using the built in parse helper function. You can also infer type definitions from zod schemas using the infer helper.

We can then use those types that we infer from zod schemas in our JSDoc comments to get type safety where we need.

If you want a deeper dive into typescript and zod, Matt Pocock has created to great free resources at Total Typescript