Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bun.serve types disallow routes and websocket being specified together #17871

Open
mayfieldiv opened this issue Mar 3, 2025 · 1 comment
Open
Assignees
Labels
bun:serve Bun.serve and HTTP server types An issue with TypeScript types

Comments

@mayfieldiv
Copy link
Contributor

I assume this is unintentional because:

  1. Bun seems to serve the routes and websocket just fine when they're both defined (as well as the fetch for the upgrade and other non-websocket routes)
  2. the static property that routes is replacing works fine alongside websocket

Example of what I'm trying to do:

import index from "./index.html";
import { Elysia } from "elysia";

const app = new Elysia(); // ...

Bun.serve({
  routes: { "/": index }, // ERROR in typescript
  // static: { "/": index }, // typescript is happy with this (though it's deprecated)
  fetch: (req, server) => {
    if (!server.upgrade(req)) {
      return app.handle(req);
    }
  },
  websocket: {
    message: (ws, message) => { /* ... */ },
  },
});

Relevant Bun code:

function serve<T, R extends { [K in keyof R]: RouterTypes.RouteValue<K & string> }>(
options: (
| (Omit<ServeOptions, "fetch"> & {
routes: R;
fetch?: (this: Server, request: Request, server: Server) => Response | Promise<Response>;
})
| (Omit<ServeOptions, "routes"> & {
routes?: never;
fetch: (this: Server, request: Request, server: Server) => Response | Promise<Response>;
})
| WebSocketServeOptions<T>
) & {
/**
* @deprecated Use `routes` instead in new code. This will continue to work for awhile though.
*/
static?: R;
},
): Server;

reload<T, R extends { [K in keyof R]: RouterTypes.RouteValue<K & string> }>(
options: (
| (Omit<ServeOptions, "fetch"> & {
routes: R;
fetch?: (this: Server, request: Request, server: Server) => Response | Promise<Response>;
})
| (Omit<ServeOptions, "routes"> & {
routes?: never;
fetch: (this: Server, request: Request, server: Server) => Response | Promise<Response>;
})
| WebSocketServeOptions<T>
) & {
/**
* @deprecated Use `routes` instead in new code. This will continue to work for awhile though.
*/
static?: R;
},
): Server;

@RiskyMH RiskyMH added types An issue with TypeScript types bun:serve Bun.serve and HTTP server labels Mar 4, 2025
@lgarron
Copy link
Contributor

lgarron commented Mar 5, 2025

I'm also unable to implement routes due to this issue. Is there any sort of short-term workaround?

EDIT: apologies, I forgot that bun add @types/bun doesn't update to the latest types if it's already installed. I might not have an issue.

@alii alii self-assigned this Mar 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bun:serve Bun.serve and HTTP server types An issue with TypeScript types
Projects
None yet
Development

No branches or pull requests

4 participants