You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
typescript [2345]: Argument of type 'ZodCoercedDate' is not assignable to parameter of type '$ZodType<any, string>'.
The types of '_zod.input' are incompatible between these types.
Type 'unknown' is not assignable to type 'string'.
The intention with this was to accept string | number | Date | null | undefined, map null -> undefined and convert undefined -> Date through the default call such that null and undefined would be converted to the current time.
Seeing this error made me think... and I ended up simplifying it with:
Handling null first and then afterwards coercing the rest. That being said, v3 typed the pipe correctly without errors, but it may overcomplicate things.
The text was updated successfully, but these errors were encountered:
Zod 4 gets a little more clever with .pipe(). This is why unknown is not always better than any: sometimes the assignability characteristics of unknown causes issues, as in this example.
I used a trick to make the z.coerce APIs a little more flexible in situations like this. Upgrade the latest beta to give it a shot. That said, your code still won't work as is for reasons that are a bit complicated.
You can either, move the default before the pipe (I believe this is functionally identical to your current schema)::
Alternatively, you can use the generic parameter of z.coerce.date (recently added) to loosen the input type and avoid the assignability issue altogether:
edit: supposedly, this is covered in the migration announcement: https://v4.zod.dev/v4/changelog#zcoerce-updates
Testing out v4 I stumbled upon a chain piping into
z.coerce.date()
.Results in,
The intention with this was to accept
string | number | Date | null | undefined
, mapnull -> undefined
and convertundefined -> Date
through the default call such thatnull
andundefined
would be converted to the current time.Seeing this error made me think... and I ended up simplifying it with:
Handling
null
first and then afterwards coercing the rest. That being said, v3 typed the pipe correctly without errors, but it may overcomplicate things.The text was updated successfully, but these errors were encountered: