Skip to content

Incorrect type returned by c.req.param() #4384

@NfNitLoop

Description

@NfNitLoop

What version of Hono are you using?

4.9.5

What runtime/platform is your app running on? (with version if possible)

deno 2.4.x (but I assume same behavior on any)

What steps can reproduce the bug?

When using a router path that may be zero-length, c.req.param() may return undefined though its types only declare string.

Example:

#!/usr/bin/env -S deno run --check -N

import { Hono } from "npm:[email protected]"

const app = new Hono()

app.get("/:remaining{.*}", (c) => {

    // Hono reports this as type `string`:
    const remaining: string = c.req.param("remaining") // ⬅️ param name also type checked.

    // However, with a root path of "/" we get `undefined`:
    const lines = [
        `type:  ${typeof remaining}`,
        `value: ${remaining}`,
    ]

    return c.text(lines.join("\n"))
})

Deno.serve({port: 8080}, app.fetch)

What is the expected behavior?

I expect:

  • Returned types match the declared types
  • I was expecting an empty string in this case.

What do you see instead?

undefined

(This can cause runtime exceptions in later code that expects the value to be a string.)

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions