From 642dd29666d866284e9c74ce7d57cf2a729e4543 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Thu, 11 Jul 2024 18:20:53 +0900 Subject: [PATCH] fix(types): `app.use(path, mw)` return correct schema type (#3128) Co-authored-by: Ame_x <121654029+EdamAme-x@users.noreply.github.com> --- src/helper/factory/index.ts | 6 +++++- src/types.test.ts | 15 +++++++++++++++ src/types.ts | 2 +- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/helper/factory/index.ts b/src/helper/factory/index.ts index 42e702f7..041b6e24 100644 --- a/src/helper/factory/index.ts +++ b/src/helper/factory/index.ts @@ -241,6 +241,10 @@ export const createFactory = (init? initApp?: InitApp }): Factory => new Factory(init) -export const createMiddleware = ( +export const createMiddleware = < + E extends Env = any, + P extends string = string, + I extends Input = {} +>( middleware: MiddlewareHandler ): MiddlewareHandler => createFactory().createMiddleware(middleware) diff --git a/src/types.test.ts b/src/types.test.ts index eafd2683..c94d1b65 100644 --- a/src/types.test.ts +++ b/src/types.test.ts @@ -2225,3 +2225,18 @@ describe('Env types and a path type with `app.use(path, handler...)` - test only }) }) }) + +// https://github.com/honojs/hono/issues/3122 +describe('Returning type from `app.use(path, mw)`', () => { + const mw = createMiddleware(async (c, next) => { + await next() + }) + it('Should not mark `*` as never', () => { + const app = new Hono().use('*', mw) + type Actual = ExtractSchema + type Expected = { + '*': {} + } + type verify = Expect> + }) +}) diff --git a/src/types.ts b/src/types.ts index e6f3cdeb..d8b7ba4b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1804,7 +1804,7 @@ export type Schema = { } type ChangePathOfSchema = keyof S extends never - ? { [K in Path]: never } + ? { [K in Path]: {} } : { [K in keyof S as Path]: S[K] } export type Endpoint = {