Created
October 31, 2020 16:04
-
-
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!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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
First downside is that I have to add extra stuff to deal with try/catch errors; the error from
catch()
is nowunknown
, previouslyany
. But it caught more valid issues, so ... seems like a weiner!Thanks a bunch!