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
When using z.preprocess, the parser's Input type is set to the wrapped parser's Input type. In most cases, this is probably incorrect, and this prevents using z.preprocess with frameworks like tRPC without type errors.
Example:
constparser=z.preprocess((data)=>Number(data),z.number());functionhandle(data: z.input<typeofparser>){returnparser.parse(data);}handle("42");// ts-error: Argument of type 'string' is not assignable to parameter of type 'number'.
^ That's a very simplified example of how this works in tRPC. I'm not sure what the ideal API is here. Maybe if we could explicitly annotate the preprocess argument? e.g.:
Or alternatively, should preprocess simply always have an Input type of unknown? This seems like it would make the most sense, because using preprocess assumes you can handle any unknown value.
Thinking about this some more, I think a better solution would be to have some kind of chain API that allows you to pipe the result of one parser to another parser:
When using
z.preprocess
, the parser'sInput
type is set to the wrapped parser'sInput
type. In most cases, this is probably incorrect, and this prevents usingz.preprocess
with frameworks like tRPC without type errors.Example:
^ That's a very simplified example of how this works in tRPC. I'm not sure what the ideal API is here. Maybe if we could explicitly annotate the preprocess argument? e.g.:
Or alternatively, should
preprocess
simply always have anInput
type ofunknown
? This seems like it would make the most sense, because using preprocess assumes you can handle any unknown value.The text was updated successfully, but these errors were encountered: