0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-21 18:18:57 +01:00

fix(types): app.use(path, mw) return correct schema type (#3128)

Co-authored-by: Ame_x <121654029+EdamAme-x@users.noreply.github.com>
This commit is contained in:
Yusuke Wada 2024-07-11 18:20:53 +09:00 committed by GitHub
parent f1c7d312a8
commit 642dd29666
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 2 deletions

View File

@ -241,6 +241,10 @@ export const createFactory = <E extends Env = any, P extends string = any>(init?
initApp?: InitApp<E>
}): Factory<E, P> => new Factory<E, P>(init)
export const createMiddleware = <E extends Env = any, P extends string = any, I extends Input = {}>(
export const createMiddleware = <
E extends Env = any,
P extends string = string,
I extends Input = {}
>(
middleware: MiddlewareHandler<E, P, I>
): MiddlewareHandler<E, P, I> => createFactory<E, P>().createMiddleware<I>(middleware)

View File

@ -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<typeof app>
type Expected = {
'*': {}
}
type verify = Expect<Equal<Expected, Actual>>
})
})

View File

@ -1804,7 +1804,7 @@ export type Schema = {
}
type ChangePathOfSchema<S extends Schema, Path extends string> = keyof S extends never
? { [K in Path]: never }
? { [K in Path]: {} }
: { [K in keyof S as Path]: S[K] }
export type Endpoint = {