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

ERROR:- TS2769: No overload matches this call. #6359

Closed
naitikprajapati1 opened this issue Feb 23, 2025 · 3 comments
Closed

ERROR:- TS2769: No overload matches this call. #6359

naitikprajapati1 opened this issue Feb 23, 2025 · 3 comments
Labels

Comments

@naitikprajapati1
Copy link

I'm trying to use an error-handling middleware with Express and TypeScript, but I'm getting a TypeScript error:

I'm trying to use an error-handling middleware with Express and TypeScript, but I'm getting a TypeScript error:

```typescript
const errorHandler = (err: any, req: Request, res: Response, next: NextFunction) => {
    res.status(500).json({ error: 'Something went wrong!' });
};

app.use(errorHandler); // Error occurs here

And the Error Message is:-
src/app.ts:30:9 - error TS2769: No overload matches this call.
  The last overload gave the following error.
    Argument of type '(err: any, req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>, next: NextFunction) => Response<...>' is not assignable to parameter of type 'PathParams'.

30 app.use(errorHandler);
           ~~~~~~~~~~~~

  node_modules/@types/express-serve-static-core/index.d.ts:157:5
    157     <
            ~
    158         P = ParamsDictionary,
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ...
    166         ...handlers: Array<RequestHandlerParams<P, ResBody, ReqBody, ReqQuery, LocalsObj>>
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    167     ): T;
        ~~~~~~~~~
    The last overload is declared here.
@bjohansebas bjohansebas added the typescript Topics related to TypeScript label Feb 23, 2025
@bjohansebas
Copy link
Member

Please provide us with the versions of the types you're using, the version of Express you're using, and an example of the code you're using that could serve as a reference

@krzysdz
Copy link
Contributor

krzysdz commented Feb 24, 2025

The error suggests that your error handler returns Response, but the errorHandler above has no return and its return type is void.

I'm assuming that you are using types for Express 5, which require return type of void | Promise<void> for [error] request handlers, the code that you really use is different from your example above and it returns Response (e.g. return res.json({ /* ... */ }). If that is the case, this comment explains the behaviour: #5987 (comment)

@naitikprajapati1
Copy link
Author

The error suggests that your error handler returns Response, but the errorHandler above has no return and its return type is void.

I'm assuming that you are using types for Express 5, which require return type of void | Promise<void> for [error] request handlers, the code that you really use is different from your example above and it returns Response (e.g. return res.json({ /* ... */ }). If that is the case, this comment explains the behaviour: #5987 (comment)

It solved Now Thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants