Skip to content
Myr

fix(request): return `string | undefined` from param() when path type is any (#4723)

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

* fix: return string | undefined from param() when path type is any When middleware uses Context with a default path type parameter (any), c.req.param('key') incorrectly returns string instead of string | undefined. This is because ParamKeys<any> resolves to string, matching the first overload that returns string. Add a new overload that detects when P is any using the 0 extends (1 & P) trick and returns string | undefined, ensuring type safety in middleware. Fixes #3198 * ci: apply automated fixes * test(request): add type tests for param() with any path type * fixed the overload and test ---------

authored by Andrew D'Amelio · committed by GitHub · parent 19d20d2 · Browse files · 3 files+25 added−2 removed
This commit

State

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

Trailers

  • Co-authored-by: exe.dev user <exedev@queen-xray.exe.xyz>
  • Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Co-authored-by: andrewdamelio <andrewdamelio@gmail.com>
  • Co-authored-by: Yusuke Wada <yusuke@kamawada.com>

Diff stats

3 files+25 added−2 removed

  • tests:+22 added−1 removed
  • other:+3 added−1 removed

3 files+25 added−2 removed

modified:src/request.test.ts+21 added
104 unmodified lines
105 [''],106 ])107 expect(req.param()).toEqual({ remaining: '' })108 })109110 describe('Type', () => {111 it('param() returns string | undefined when P is any (middleware context)', () => {112 // When middleware uses Context without an explicit path type, P defaults to any.113 // param(key) should return string | undefined, not string.114 const rawRequest = new Request('http://localhost/users/123')115 const req = new HonoRequest<any>(rawRequest, '/users/123', [116 [[[undefined, {} as RouterRoute], { id: '123' }]],117 ])118 expectTypeOf(req.param('id')).toEqualTypeOf<string | undefined>()119 })120121 it('param() returns string when P is a known route string', () => {122 // When P is a concrete route, named params should still return string (non-optional).123 const rawRequest = new Request('http://localhost/123')124 const req = new HonoRequest<'/:id'>(rawRequest, '/123', [125 [[[undefined, {} as RouterRoute], { id: '123' }]],126 ])127 expectTypeOf(req.param('id')).toEqualTypeOf<string>()128 })129 })130})131132describe('matchedRoutes', () => {133 test('req.routePath', () => {
392 unmodified lines
modified:src/request.ts+3 added-1 removed
89 unmodified lines
90 * // or all parameters at once91 * const { id, comment_id } = c.req.param()92 * ```93 */94 param<P2 extends ParamKeys<P> = ParamKeys<P>>(key: P2 extends `${infer _}?` ? never : P2): string94 param<P2 extends ParamKeys<P> = ParamKeys<P>>(95 key: string extends P ? never : P2 extends `${infer _}?` ? never : P296 ): string97 param<P2 extends RemoveQuestion<ParamKeys<P>> = RemoveQuestion<ParamKeys<P>>>(98 key: P299 ): string | undefined100 param(key: string): string | undefined
389 unmodified lines
modified:src/types.test.ts+1 added-1 removed
521 unmodified lines
522 const handler: Handler<E> = (c) => {523 const foo = c.get('foo')524 expectTypeOf(foo).toEqualTypeOf<number>()525 const id = c.req.param('id')526 expectTypeOf(id).toEqualTypeOf<string>()526 expectTypeOf(id).toEqualTypeOf<string | undefined>()527 return c.text('Hi')528 }529 app.get('/', handler)530 const res = await app.request(url)
3286 unmodified lines