Skip to content
Myr

fix(types): infer response type from last handler in app.on 9- and 10-handler overloads (#4865)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The 9- and 10-handler `app.on` overloads declared `R` on the last handler position but discarded it, hardcoding `MergeTypedResponse<HandlerResponse<any>>` in the schema instead. This caused the inferred `output` to collapse to `{} | Promise<void>` regardless of what the response handler returned. Affected overloads: - app.on(method, path, handler x10) - app.on(method[], path, handler x9) - app.on(method[], path, handler x10) The 8-handler variants were already using `MergeTypedResponse<R>` correctly.

authored by tako._.v · committed by GitHub · parent c37ba26 · Browse files · 2 files+50 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+50 added−3 removed

  • tests:+47 added−0 removed
  • other:+3 added−3 removed

2 files+50 added−3 removed

modified:src/types.test.ts+47 added
399 unmodified lines
400 }401 }402 type verify = Expect<Equal<Expected, Actual>>403 })404405 test('app.on(method, path, ...10 handlers) - last handler response type should be inferred', () => {406 const noop: MiddlewareHandler = async (_c, next) => {407 await next()408 }409 const route = app.on('GET', '/x10', noop, noop, noop, noop, noop, noop, noop, noop, noop, (c) =>410 c.json({ success: true })411 )412 type Actual = ExtractSchema<typeof route>['/x10']['$get']['output']413 type Expected = { success: true }414 type verify = Expect<Equal<Expected, Actual>>415 })416417 test('app.on(method[], path, ...9 handlers) - last handler response type should be inferred', () => {418 const noop: MiddlewareHandler = async (_c, next) => {419 await next()420 }421 const route = app.on(['GET'], '/x9-arr', noop, noop, noop, noop, noop, noop, noop, noop, (c) =>422 c.json({ success: true })423 )424 type Actual = ExtractSchema<typeof route>['/x9-arr']['$get']['output']425 type Expected = { success: true }426 type verify = Expect<Equal<Expected, Actual>>427 })428429 test('app.on(method[], path, ...10 handlers) - last handler response type should be inferred', () => {430 const noop: MiddlewareHandler = async (_c, next) => {431 await next()432 }433 const route = app.on(434 ['GET'],435 '/x10-arr',436 noop,437 noop,438 noop,439 noop,440 noop,441 noop,442 noop,443 noop,444 noop,445 (c) => c.json({ success: true })446 )447 type Actual = ExtractSchema<typeof route>['/x10-arr']['$get']['output']448 type Expected = { success: true }449 type verify = Expect<Equal<Expected, Actual>>450 })451})452453describe('TypedResponse', () => {454 test('unknown', () => {
3409 unmodified lines
modified:src/types.ts+3 added-3 removed
1788 unmodified lines
1789 H<E11, MergedPath, I10, R>,1790 ]1791 ): HonoBase<1792 IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11]>,1793 S & ToSchema<M, MergePath<BasePath, P>, I10, MergeTypedResponse<HandlerResponse<any>>>,1793 S & ToSchema<M, MergePath<BasePath, P>, I10, MergeTypedResponse<R>>,1794 BasePath,1795 MergePath<BasePath, P>1796 >1797
296 unmodified lines
2094 H<E10, MergedPath, I9, R>,2095 ]2096 ): HonoBase<2097 IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9, E10]>,2098 S & ToSchema<M, MergePath<BasePath, P>, I9, MergeTypedResponse<HandlerResponse<any>>>,2098 S & ToSchema<M, MergePath<BasePath, P>, I9, MergeTypedResponse<R>>,2099 BasePath,2100 MergePath<BasePath, P>2101 >2102
39 unmodified lines
2142 H<E11, MergedPath, I10, R>,2143 ]2144 ): HonoBase<2145 IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11]>,2146 S & ToSchema<M, MergePath<BasePath, P>, I10, MergeTypedResponse<HandlerResponse<any>>>,2146 S & ToSchema<M, MergePath<BasePath, P>, I10, MergeTypedResponse<R>>,2147 BasePath,2148 MergePath<BasePath, P>2149 >2150
338 unmodified lines