So, I have this interface for my Express controller handler.
interface Handler<
Body extends ZodObject,
Query extends ZodObject,
Params extends ZodObject,
> {
path?: string;
method: "get" | "post" | "put" | "patch" | "delete";
validators?: {
body?: Body;
};
handler: RequestHandler<z.infer<Body>>;
}
What I want is that the type of Body should be inferred from the object I set at validator.body. Let’s say I set it to z.object({ x: z.string() })
, then I want to the req
parameter of handler
function to have req.body
as { x: string }
, but I can’t make it happen.
I tried searching for it on Google and even tried GPT and Gemini but none can solve this problem. I just want to know whether this can be done or not, and if yes then how.