DEV Community

Discussion on: Boost Your JavaScript with JSDoc Typing

Collapse
 
mroeling profile image
Mark Roeling

wondering: does it help in "parsing" runtime, in a browser? What is the effect perfomance-wise?

Collapse
 
samuel-braun profile image
Samuel Braun • Edited

Not sure exactly what you mean with parsing at runtime. JSDoc wasn't meant to be run in the browser. But if you want to validate types for exmaple of an API you could use something like Zod. With Zod you can also type your code using the same schema a JSDoc:

const myUserSchema = z.object({
    user: z.string(),
    age: z.number(),
});
// @type {z.infer<typeof myUserSchema>}
const user = await fetch("user.json");

// Validate if data is actually same type as defined in the schema
// Throws an error if not
myUserSchema.parse(user);
Enter fullscreen mode Exit fullscreen mode