Enforcing strict types for Hono's c.json() responses?
Is it possible to enforce strict typing for Hono's
c.json()
responses at a top level (hono.Handler<..>
)?
Currently, I can define expected response types like this:
This provides suggestions in c.json({...})
, but doesn't raise a type error for mismatched types:
Hono's internal types for quick reference:
Example: https://github.com/builder-group/community/blob/develop/examples/openapi-router/express/petstore/src/router.ts
Code: https://github.com/builder-group/community/blob/develop/packages/openapi-router/src/types/features/hono.tsGitHub
community/examples/openapi-router/express/petstore/src/router.ts at...
A collection of open source libraries by builder.group. Let's build together. - builder-group/community
GitHub
community/packages/openapi-router/src/types/features/hono.ts at dev...
A collection of open source libraries by builder.group. Let's build together. - builder-group/community
4 Replies
Thanks, will check it out 🙂
Does this also work on the
hono.Handler
level? Because it doesn't seem so. I get the type suggested by the IDE (see image) but not enforced, meaning that I don't get an error if its wrong. I guess its because of Response | TypedResponse<O>
in the HandlerResponse
?
Hono's internal types for quick reference:
Wait I can just directly pass the
hono.TypedResponse
instead of going through the hono.HandlerResponse
.
Now its working. Not perfect since the type gets enforced on the handler level (not at c.json
) but better than before. Thanks for the hint 🙂