Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript Error: No overload matches this call in Express route handler

I'm working on an Express.js application with TypeScript, and I'm encountering a type error that I can't resolve. Here's the relevant code:

import express from 'express';
import Stripe from 'stripe';
import { config } from '../config';
import { Request, Response } from 'express';

const router = express.Router();

// ... other code ...

router.post('/create-checkout-session', async (req: Request, res: Response) => {
  // Route handler implementation
});

export default router;

When I try to compile this, I get the following error:

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

The error seems to be related to the types of the req and res parameters in the route handler. I've imported Request and Response types from 'express', but the error persists. Here are my questions: What's causing this type mismatch? How can I correctly type the route handler to resolve this error? Are there any specific TypeScript configurations or type definitions I should be aware of when working with Express? Any help or insights would be greatly appreciated. Thank you!

I've tried and observed: I imported the Request and Response types from Express and used them to type the route handler parameters. I expected this to resolve the type mismatch, as these types should be compatible with Express's router methods. However, the error persists, suggesting that there might be an issue with how the Express types are being interpreted or that there's a mismatch between the Express version and the type definitions being used

like image 615
Val Labs Avatar asked Nov 17 '25 16:11

Val Labs


2 Answers

dont return the res. instead use void return after res. like

res.status(400).send({ message: 'You are not valid for this website.' });
return;
like image 73
Samiul Karim Avatar answered Nov 20 '25 14:11

Samiul Karim


please downgrade the @types/express package to fix this. you can do so by doing npm i -D @types/express@4

like image 23
Chitrakshi Avatar answered Nov 20 '25 14:11

Chitrakshi