i ve got a question about `pipe` and
i've got a question about
pipe
and coerce
. the shape of my data is a bit funky so sorry if it's a bit confusing.
the input for the schema is an array of strings, that will either a number like ["7"]
or an iso date (without time data), like ["2023-01-01", "2023-02-02"]
.
i've currently got the schema as: array(string().pipe(union([coerce.number(), coerce.date()])))
with the intention that it will accept a string, then coerce it to either a number or a date.
the coercion itself is working fine, however the inferred type of this schema is a bit puzzling to me, as it's telling me the type of the schema is number[] | Date[]
, when i would have thought it would be string[]
because i'm explicitly feeding string()
into array()
.
i'm assuming this is intended behaviour but i guess i'm just curious why it isn't string[]
when i'm explicitly stating that it should be5 Replies
![No description](https://utfs.io/f/4661766f-f7ff-4f46-ad92-fd0f39cac2ad-le3u4t.41.192x.png)
trying with transform i get a similar result. my thinking must be incorrect 😅
maybe someone knows how i should be approaching this
![No description](https://utfs.io/f/85825f2d-5976-416a-9124-233ee378e511-le3u4v.31.552x.png)
Not sure I understand, but try to use
z.input
instead of z.infer
(which just another name for z.output
)?z.input
existed, this is exactly what i want. thank you very much 😄There you go
Have to say I kind of wish
zod
would've just not included z.infer
to begin with, as z.input
and z.output
is just so much clearer about what it does. maybe z.input
and z.output
actually wasn't added until later though, so maybe z.infer
is the original and therefore has to be kept for backwards compatibility, but yeah