-
-
Notifications
You must be signed in to change notification settings - Fork 544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When chaining routes with middlewares, the context type might be incorrect. #2019
Comments
Thank you for pointing that out. Just to be sure, I've confirmed that the same issue occurs in the v4 branch as well. |
Hi @agatan ! Perhaps the specifications here are wrong. Lines 841 to 845 in 1f46aa1
This test must fail, and the following tests must pass: type Env = {
Variables: {
init: number
}
}
const app1 = new Hono<Env>().get('/', mw1, (c) => {
expectTypeOf(c.get('init')).toEqualTypeOf<number>()
expectTypeOf(c.var.init).toEqualTypeOf<number>()
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
return c.json(0)
})
app1.get('/', (c) => {
expectTypeOf(c.get('init')).toEqualTypeOf<number>()
expectTypeOf(c.var.init).toEqualTypeOf<number>()
// @ts-expect-error foo1 is not typed
c.get('foo1')
// @ts-expect-error foo1 is not typed
c.var.foo1
return c.json(0)
}) To correct this, we can fix the following type definitions. Lines 384 to 398 in 1f46aa1
The diff looks like this: diff --git a/src/types.ts b/src/types.ts
index 5fdcccd..2d6ffd0 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -391,11 +391,7 @@ export interface HandlerInterface<
>(
path: P,
handler: H<E2, MergedPath, I, R>
- ): Hono<
- IntersectNonAnyTypes<[E, E2]>,
- S & ToSchema<M, MergePath<BasePath, P>, I['in'], MergeTypedResponseData<R>>,
- BasePath
- >
+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I['in'], MergeTypedResponseData<R>>, BasePath>
// app.get(path, handler x2)
< What do you think? I think we can fix this by the approach. |
@yusukebe I was trying it in this commit, but as you pointed out, the test at Lines 841 to 845 in 1f46aa1
If we proceed with this solution, it looks like the commit I tried could be reused. I'll create a pull request. |
Yeah. I think the tests should fail. I've left comments on your PR. Check them! |
Fixed! |
What version of Hono are you using?
3.12.5
What runtime/platform is your app running on?
Node
What steps can reproduce the bug?
Run and type check the code below:
What is the expected behavior?
In the handler for
/nothing
, no middlewares are applied.So, the type of
c.var
should be inferred as an empty object.What do you see instead?
In the handler for
/nothing
, the type ofc.var
is inferred as{ foo1: string; foo2: string }
.Additional information
I believe the issue with Hono lies in the overloaded types of HandlerInterface.
The HandlerInterface returns Hono<E, S, P>, where E is extended by the Env definitions of handlers. However, this extension is limited to the same handler scope.
I attempted a fix (see commit), but then realized that this might conflict with issue #1666.
I believe that to achieve correct type inference, it's necessary to maintain information about the paths where middleware is applied at the type level. However, I think this could be quite challenging to implement.
The text was updated successfully, but these errors were encountered: