H
Hono5w ago
Gauts

Can not get the Types in route fn

Hi, I am trying to learn this framework. But I am having some difficulty here, and I think you can help me. This way I can get the Types from the valid() fn.
users.get("/author", authGuard, zValidator("query", schema), (c) => {
const data = c.req.valid("query"); <--- Types Available
});
users.get("/author", authGuard, zValidator("query", schema), (c) => {
const data = c.req.valid("query"); <--- Types Available
});
But like this I can't, why?
const fn = async (c) => {
const data = c.req.valid("query"); <--- Types Unavailable
};
users.get("/author", authGuard, zValidator("query", schema), fn;
const fn = async (c) => {
const data = c.req.valid("query"); <--- Types Unavailable
};
users.get("/author", authGuard, zValidator("query", schema), fn;
2 Replies
blair
blair5w ago
This isn't a hono or framework thing, this is a typescript thing You've declared an async function that takes one argument c, without a type declaration, so correct typescript has no idea what c.req.valid("query") is as it doesn't even know what c is
blair
blair5w ago
Best Practices - Hono
Web framework built on Web Standards for Cloudflare Workers, Fastly Compute, Deno, Bun, Vercel, Node.js, and others. Fast, but not only fast.

Did you find this page helpful?