Pdata
Bun

property

WebSocketHandler.data

data?: T

Specify the type for the ServerWebSocket.data property on connecting websocket clients. You can pass this value when you make a call to Server.upgrade.

This pattern exists in Bun due to a TypeScript limitation (#26242)

Bun.serve({
  websocket: {
    data: {} as { name: string }, // ← Specify the type of `ws.data` like this
    message: (ws, message) => console.log(ws.data.name, 'says:', message);
  },
  // ...
});