Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
trying to use zod in js with jsdoc type comments for vs code - almost works!
'use strict'
// examples from https://github.com/vriad/zod
// trying to use zod in JS w/ jsdoc type comments in vsCode
const z = require('zod')
const dogSchema = z.object({
name: z.string(),
neutered: z.boolean(),
})
/** @typedef { z.infer<typeof dogSchema> } Dog */
/** @type { Dog } */
const cujo = dogSchema.parse({
name: 'Cujo',
neutered: true,
})
console.log(cujo)
/** @type { Dog } */
const cujo2 = {
name: 'Cujo2',
neutered: false
}
console.log(cujo2)
try {
/** @type { Dog } */
dogSchema.parse({
name: 'Fido',
})
} catch (err) {
console.log(err.message)
}
// the following should be an type error, but Dog is defined in vsCode
// as a "partial", but shouldn't be
/** @type { Dog } */
const fido2 = {
name: 'Fido2'
}
console.log(fido2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment