From 8bf4d5c78ff95c943c2772f9eeddb72d8266dd12 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Sun, 5 Jan 2025 18:00:47 +0900 Subject: [PATCH] fix(types): correct `app.on(method,path[],middleware,handler)` type --- src/types.test.ts | 8 ++++++++ src/types.ts | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/types.test.ts b/src/types.test.ts index 9a4720029..d1ec00107 100644 --- a/src/types.test.ts +++ b/src/types.test.ts @@ -277,6 +277,14 @@ describe('OnHandlerInterface', () => { } type verify = Expect> }) + + test('app.on(method, path[], middleware, handler) should not throw a type error', () => { + const middleware: MiddlewareHandler<{ Variables: { foo: string } }> = async () => {} + app.on('GET', ['/a', '/b'], middleware, (c) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + return c.json({}) + }) + }) }) describe('TypedResponse', () => { diff --git a/src/types.ts b/src/types.ts index f58f162bd..bfa96a260 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1745,10 +1745,10 @@ export interface OnHandlerInterface< ): HonoBase, I, MergeTypedResponse>, BasePath> // app.on(method | method[], path[], ...handlers[]) - = any>( + = any, E2 extends Env = E>( methods: string | string[], paths: string[], - ...handlers: H[] + ...handlers: H[] ): HonoBase>, BasePath> }