express icon indicating copy to clipboard operation
express copied to clipboard

No overload matches this call error ts(2769)

Open vishalsingh2972 opened this issue 7 months ago • 1 comments

This is my indexWithZod.ts file:

import { z } from 'zod';

export const appWithZod = express();
appWithZod.use(express.json());


const sumInput = z.object({
  a: z.number(),
  b: z.number()
});

appWithZod.post('/sum', (req, res) => {

  const parsedResponse = sumInput.safeParse(req.body);

  if (!parsedResponse.success) {
    return res.status(411).json({
      message: 'Incorrect inputs'
    });
  }
  
  const answer = parsedResponse.data.a + parsedResponse.data.b;

  return res.json({
    answer
  });
});

I am getting error at (req, res) => { where the exact error message is

No overload matches this call. The last overload gave the following error. Argument of type '(req: Request<{}, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>, number>) => Response<any, Record<...>, number>' is not assignable to parameter of type 'Application<Record<string, any>>'. Type '(req: Request<{}, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>, number>) => Response<any, Record<...>, number>' is missing the following properties from type 'Application<Record<string, any>>': init, defaultConfiguration, engine, set, and 63 more.ts(2769)

Here is the additional bin.ts file for reference:

import { appWithZod } from './indexWithZod';

appWithZod.listen(3001, () => {
  console.log('Server (appWithZod) is running on PORT 3001');
});

vishalsingh2972 avatar May 30 '25 13:05 vishalsingh2972

Your problem lies at: return res.status(411).json({ message: 'Incorrect inputs' }); and return res.json({ answer });. Removing the return statements should fix your problem, the TS error clarifies that you passed a (req, res) => Response type instead of an Application type. You can just call res.status and json then return to exit the function.

omerbenda avatar Jun 07 '25 21:06 omerbenda

This should be fixed with the latest version of the express-server-static types.

bjohansebas avatar Jul 15 '25 02:07 bjohansebas