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
typeUserContext={Variables: {userId: string}}// Works as expected - both middlewares maintain correct typeconstapp1=newHono().get("/ws",createMiddleware<UserContext>(async(c,next)=>{c.set('userId',"12345")awaitnext()}),async(c,next)=>{// c is correctly inferred as Context<UserContext, "/ws", {}>awaitnext();});// Type inference breaks in the middle middlewareconstapp2=newHono().get("/ws",createMiddleware<UserContext>(async(c,next)=>{c.set('userId',"12345")awaitnext()}),async(c,next)=>{// c is incorrectly inferred as Context<BlankEnv, "/ws", {}>awaitnext();},async(c,next)=>{// c is correctly inferred as Context<UserContext, "/ws", {}>awaitnext();});
What is the expected behavior?
The UserContext type should be maintained throughout the entire middleware chain, regardless of the number of middlewares.
What do you see instead?
In a chain with more than two middlewares, intermediate middlewares lose the custom context type and revert to BlankEnv, while the last middleware maintains the correct type.
Additional information
It's possible to type the Hono instance itself with new Hono<UserContext>().
While this approach fixes the type inference, it introduces a type safety issue. The variables become accessible in all middlewares, even before they are actually set. For example, if userId is only set in the second middleware, the type system would incorrectly allow access to it in the first middleware where it hasn't been initialized yet.
The text was updated successfully, but these errors were encountered:
What version of Hono are you using?
4.6.8
What runtime/platform is your app running on?
Bun
What steps can reproduce the bug?
What is the expected behavior?
The
UserContext
type should be maintained throughout the entire middleware chain, regardless of the number of middlewares.What do you see instead?
In a chain with more than two middlewares, intermediate middlewares lose the custom context type and revert to
BlankEnv
, while the last middleware maintains the correct type.Additional information
It's possible to type the Hono instance itself with
new Hono<UserContext>()
.While this approach fixes the type inference, it introduces a type safety issue. The variables become accessible in all middlewares, even before they are actually set. For example, if userId is only set in the second middleware, the type system would incorrectly allow access to it in the first middleware where it hasn't been initialized yet.
The text was updated successfully, but these errors were encountered: