Skip to content

Add route-level generic Request/Response typing to TypeScript definitions#320

Open
Copilot wants to merge 2 commits into
mainfrom
copilot/support-generic-request-response-types
Open

Add route-level generic Request/Response typing to TypeScript definitions#320
Copilot wants to merge 2 commits into
mainfrom
copilot/support-generic-request-response-types

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jun 1, 2026

The current TypeScript declarations do not allow route handlers to express narrowed request shapes or enforce response body contracts. This change introduces non-breaking generics across the handler/middleware/route type surface so routes can opt into strongly typed input/output.

  • Type surface: generic request/response plumbing

    • Middleware, ErrorHandlingMiddleware, and HandlerFunction now accept <TRequest, TResponse> generics (defaulting to existing Request/Response behavior).
    • API route registration methods (get, post, put, patch, delete, options, head, any, METHOD) and use overloads now carry those generics to route-level declarations.
  • Response body contract typing

    • Response is now generic: Response<TBody = any>.
    • send, json, and jsonp are typed to TBody, enabling compile-time enforcement of response payload shape.
  • Type tests (tsd) for generic behavior

    • Added tests for:
      • extended Request usage in typed handlers,
      • typed Response<TBody> payload enforcement,
      • route registration with explicit generic request/response types,
      • rejection of invalid response payload shapes.
interface TypedRequest extends Request {
  params: { thingId: string; anotherThingId: string };
}

interface TypedResponseBody {
  hello: string;
  foo: string;
}

api.get<TypedRequest, Response<TypedResponseBody>>('/typed', (req, res) => {
  res.json({
    hello: req.params.thingId,
    foo: req.params.anotherThingId,
  });
});

Copilot AI changed the title [WIP] Add support for generic request and response types in Typescript Add route-level generic Request/Response typing to TypeScript definitions Jun 1, 2026
Copilot AI requested a review from naorpeled June 1, 2026 19:11
@naorpeled naorpeled marked this pull request as ready for review June 1, 2026 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature request] Typescript - Support generic request / response types

2 participants