Skip to content

Commit

Permalink
fix(types): app.use(path, mw) return correct schema type (#3128)
Browse files Browse the repository at this point in the history
Co-authored-by: Ame_x <121654029+EdamAme-x@users.noreply.github.com>
  • Loading branch information
yusukebe and EdamAme-x authored Jul 11, 2024
1 parent f1c7d31 commit 642dd29
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/helper/factory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
15 changes: 15 additions & 0 deletions src/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>>
})
})
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 642dd29

Please sign in to comment.