Closed
Description
I'm using React Router as a...
framework
Reproduction
Using the following code:
// root.tsx
const whateverMiddleware: Route.unstable_MiddlewareFunction = async (
{ request, params, context },
next,
) => {
// do some real stuff prior to loaders running, no post processing necessary
console.log("foo");
// now it should NOT be necessary to manually call next() or return the response
};
export const unstable_middleware = [whateverMiddleware];
Will produce type errors when running a typecheck:
app/root.tsx:45:7 - error TS2322: Type '({ request, params, context }: ServerDataFunctionArgs<Info>, next: unstable_MiddlewareNextFunction<Response>) => Promise<void>' is not assignable to type 'unstable_MiddlewareFunction'.
Type 'Promise<void>' is not assignable to type 'MaybePromise<Response | undefined>'.
Type 'Promise<void>' is not assignable to type 'Promise<Response | undefined>'.
Type 'void' is not assignable to type 'Response | undefined'.
The only way I found to fix this is to explicitly call the next() function and return the result like this:
// root.tsx
const whateverMiddleware: Route.unstable_MiddlewareFunction = async (
{ request, params, context },
next,
) => {
// do some real stuff prior to loaders running, no post processing necessary
console.log("foo");
return await next();
};
export const unstable_middleware = [whateverMiddleware];
System Info
System:
OS: Linux 5.15 Ubuntu 22.04.5 LTS 22.04.5 LTS (Jammy Jellyfish)
CPU: (12) x64 13th Gen Intel(R) Core(TM) i7-1355U
Memory: 4.39 GB / 7.76 GB
Container: Yes
Shell: 5.8.1 - /usr/bin/zsh
Binaries:
Node: 22.14.0 - ~/develop/neebo/.devbox/nix/profile/default/bin/node
npm: 10.9.2 - ~/develop/neebo/.devbox/nix/profile/default/bin/npm
pnpm: 9.15.5 - ~/develop/neebo/.devbox/virtenv/nodejs/corepack-bin/pnpm
Browsers:
Chrome: 129.0.6668.89
npmPackages:
@react-router/dev: ^7.4.0 => 7.4.0
@react-router/express: ^7.4.0 => 7.4.0
@react-router/node: ^7.4.0 => 7.4.0
react-router: ^7.4.0 => 7.4.0
vite: ^5.1.0 => 5.4.14
Used Package Manager
pnpm
Expected Behavior
Typecheck is successful. According to your decision record calling next()
and returning the response should not be necessary if you don't want to do any post processing. Could that be related to the void/undefined change in v7.4.0?
Actual Behavior
app/root.tsx:45:7 - error TS2322: Type '({ request, params, context }: ServerDataFunctionArgs<Info>, next: unstable_MiddlewareNextFunction<Response>) => Promise<void>' is not assignable to type 'unstable_MiddlewareFunction'.
Type 'Promise<void>' is not assignable to type 'MaybePromise<Response | undefined>'.
Type 'Promise<void>' is not assignable to type 'Promise<Response | undefined>'.
Type 'void' is not assignable to type 'Response | undefined'.