neurotech - Hi everyone. I'm struggling with zo...
Hi everyone. I'm struggling with zod 4 and how it handles coercing values. This is my scenario:
I have a form that contains a "Port"
input
that has a type
of number
.
AFAICT, the typing of this input's value is a string, so I need zod
to coerce it to number in order to validate properly against this schema:
In my form, I have a call to form.watch
:
I use these values in a separate component, and this is where I'm struggling. The type of port
returned from form.watch
is unknown
, even though I'm coercing it to a number.
Am I going about this the wrong way, or is there something I'm missing?9 Replies
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
Passing those generics shouldn't be needed anymore but inference for the zodResolver doesn't seem to be working on tsplayground
but the issue here is that
z.coerce
results in a type with unknown
as input
and watch
gives you unparsed/non transformed values
which in this case would be of type unknown
Part of the reason why I'm personally not huge on coerce. You could make use of zod's new codecs if you're on the latest versionSolution
Thanks @janglad - I ended up doing this:
I can bump to the latest and look into codecs though
well by doing that you now have a schema that (at least on the type level) takes in a number and returns a number right
Have a look at the result you get from watch, it should be a string
The type of
port
from form.watch
is number
😮At runtime?
I’d swear watch gives non transformed values haha
But might be wrong
not sure at runtime, but the type in my intellisense is number
Aah right, yeah that makes sense 🙂
Since the input type is also number