Skip to content

Instantly share code, notes, and snippets.

@pmuellr
Created October 31, 2020 16:04
Show Gist options
  • Save pmuellr/60668d33049f96ce7323f5eab648f468 to your computer and use it in GitHub Desktop.
Save pmuellr/60668d33049f96ce7323f5eab648f468 to your computer and use it in GitHub Desktop.
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)
@pmuellr
Copy link
Author

pmuellr commented Jun 8, 2023

First downside is that I have to add extra stuff to deal with try/catch errors; the error from catch() is now unknown, previously any. But it caught more valid issues, so ... seems like a weiner!

Thanks a bunch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment