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
The Envs from middleware is inferred in the handlers which are created as app.get('/foo', middleware, ...), but it isn't when you create them using factory.createHandlers(middleware, ...).
import{Hono}from'hono';import{createFactory,createMiddleware}from'hono/factory'import{logger}from'hono/logger'constapp=newHono();constfactory=createFactory()constmiddleware=createMiddleware<{Variables: {foo: string}}>(async(c,next)=>{c.set('foo','bar')awaitnext()})consthandlers=factory.createHandlers(logger(),middleware,async(c)=>{// I suppose `c` is `Context<{ Variables: { foo: string } }, ...>`, but actually `Context<any, any, {}>`returnc.json(c.get('foo'));});app.get('/api', ...handlers).get('/api',logger(),middleware,async(c)=>{// Here `c` is inferred as expected.returnc.json(c.get('foo'));})
I attach how it looks in VSCode for reference
What is the expected behavior?
As I wrote in the code comment, the context type of c in createHandlers is inferred as Context<{ Variables: { foo: string } }, ...> so I can use c.get('foo') or c.var.foo with the inferred type, as like as it works for the handler which directly attached to app.
What do you see instead?
The type of c is Context<any, any, {}> so I cannot get benefit from the type. Although it works as expected for the directly attached handler.
Additional information
I am not 100% sure if my assumption that the inference works for the createHandlers. If it is not expected as design (or TypeScript constraint), sorry for my misunderstanding.
I have searched the issue and found #3202 (and its dups - #3341, #3198) is discussing the inference for middlewares, but I guess it is actually not relevant.
The text was updated successfully, but these errors were encountered:
Thank you for raising the issue. This is like a bug, but it is a not implemented thing rather than a bug. We can't fix it right now, but we may do it later.
Thank you for raising the issue. This is like a bug, but it is a not implemented thing rather than a bug. We can't fix it right now, but we may do it later.
Great! This is reassuring. I also encountered the same issue.
What version of Hono are you using?
4.6.3
What runtime/platform is your app running on?
Node.js / Bun
What steps can reproduce the bug?
The
Env
s from middleware is inferred in the handlers which are created asapp.get('/foo', middleware, ...)
, but it isn't when you create them usingfactory.createHandlers(middleware, ...)
.Here's an example based on the doc:
I attach how it looks in VSCode for reference
What is the expected behavior?
As I wrote in the code comment, the context type of
c
increateHandlers
is inferred asContext<{ Variables: { foo: string } }, ...>
so I can usec.get('foo')
orc.var.foo
with the inferred type, as like as it works for the handler which directly attached toapp
.What do you see instead?
The type of
c
isContext<any, any, {}>
so I cannot get benefit from the type. Although it works as expected for the directly attached handler.Additional information
I am not 100% sure if my assumption that the inference works for the
createHandlers
. If it is not expected as design (or TypeScript constraint), sorry for my misunderstanding.I have searched the issue and found #3202 (and its dups - #3341, #3198) is discussing the inference for middlewares, but I guess it is actually not relevant.
The text was updated successfully, but these errors were encountered: