hono
hono copied to clipboard
Incorrect type returned by c.req.param()
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
@NfNitLoop
Thanks! This is a bug.
[This change updates the parameter extraction logic to return an empty string ("") instead of undefined for empty matches, ensuring runtime behavior matches the declared types and preventing potential runtime errors.
Also adds a test to verify this behavior.](https://github.com/honojs/hono/pull/4395)