When to use Zod?
I'v seen the youtube hype for Zod now, but it still unclear to me when I should actually use Zod.
Most of the examples I find online use Zod for something like API-data or user input data (like forms). But I use TS for so much more (well try to any way, still learning 😉). Should I just forget that TS exists and use Zod for everything I used to do in TS or should I use TS when not dealing with API-data or user input?
20 Replies
TS doesn't have runtime validation
Yes I know this, but most of the time I don't really need that.
that's very true
well maybe I do 🤷♀️
but it's one of those things, that it comes up often enough
that integrating it into your app makes sense almost always
and then if you can integrate that into react query, etc
it makes for a really nice dx
Yes, I am using a horrible CMS so I get most of my data from the CMS as pure HTML. here Zod can only help me see if I get a string in runtime.
and all the runtime validation comes "for free" so to speak
so do you use zod for "everything"? or just some things
me?
I use it basically always
when I can
I'll try to write the entire app with Zod then🤔
Theo had a good bit about this, I remember seeing it on youtube
lemme dig it up for you
I've probably seen it. but even he mostly used it for API data or user input. Seems more verbose for simple components that take a string or a few strings as inputs
here it is
Theo - ping․gg
YouTube
You Might Be Using Typescript Wrong...
I'm getting increasingly tired of the "but TS is so much woooooork" comments so I figured I'd do a rant.
If you have friends or coworkers who are using Typescript incorrectly, please send this video to them :)
Also please use Zod more it's very good https://github.com/colinhacks/zod
Join the Discord: https://t3.gg/discord
Follow me on Twitter...
?
I think I'm missing something....
Don't confuse me with someone who knows what they are talking about.
To me it just seemed like I needed more stuff in my code with things that are one-liners to type in TS. More stuff = more stuff to maintain/read
If its data thats coming from the app just use typescript, if you are interacting with 3rd party (app,api,forms etc) use zod
So if you absolutely know your types, then sure, you don't need zod
So you still generally use typescript but zod is for being able to ensure this data is shaped like this
which typescript wont give you
what Keef said