Skip to content

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

@rinsuki

Description

@rinsuki

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

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