honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
19.02k stars 539 forks source link

With `UpgradeWebSocket`, any path parameters become non-optional in TS type system #3198

Open rinsuki opened 2 months ago

rinsuki commented 2 months ago

What version of Hono are you using?

4.5.1

What runtime/platform is your app running on?

Node.js

What steps can reproduce the bug?

import { Hono } from "hono"
import { UpgradeWebSocket } from "hono/ws"

declare var upgradeWebSocket: UpgradeWebSocket // actually I'm using `@hono/node-ws` one, but `@hono/node-ws` exposing UpgradeWebSocket as-is

const app = new Hono()

app.get("/api/rooms/:roomId/info", async c => {
    const roomId: string = c.req.param("roomId")
    // @ts-expect-error
    const yetAnotherParam: string = c.req.param("yetAnotherParam")

    return c.json({"name": "stub"})
})

app.get("/api/rooms/:roomId/ws", upgradeWebSocket(c => {
    const roomId: string = c.req.param("roomId")
    // @ts-expect-error
    const yetAnotherParam: string = c.req.param("yetAnotherParam")

    return {}
}))

What is the expected behavior?

In the example code, c.req.param("yetAnotherParam") should return string | undefined in both handlers (all of // @ts-expect-error should be consumed).

What do you see instead?

In the example code, c.req.param("yetAnotherParam") returns a string in the handler using UpgradeWebSocket.

Additional information

No response

yusukebe commented 2 months ago

Hi @rinsuki

Thank you for the issue. This is related to #3202; if #3202 is resolved, this issue can be fixed.