question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

couldn't use multer as router middleware with typescript

See original GitHub issue

example

// router.ts
import { Router } from "express";
import multer from "multer";

const router = Router();
const upload = multer({
  storage: multer.memoryStorage()
});

router.post("/", [upload.any(), someEndpoint]);  // this couldn't work

it says

No overload matches this call.
  Overload 1 of 3, '(path: PathParams, ...handlers: RequestHandler<ParamsDictionary, any, any>[]): Router', gave the following error.
    Argument of type 'RequestHandler<ParamsDictionary, any, any, any>[]' is not assignable to parameter of type 'RequestHandler<ParamsDictionary, any, any>'.
      Type 'RequestHandler<ParamsDictionary, any, any, any>[]' provides no match for the signature '(req: Request<ParamsDictionary, any, any>, res: Response<any>, next: NextFunction): any'.
  Overload 2 of 3, '(path: PathParams, ...handlers: RequestHandlerParams<ParamsDictionary, any, any>[]): Router', gave the following error.
    Type 'RequestHandler<ParamsDictionary, any, any, any>' is not assignable to type 'ErrorRequestHandler<ParamsDictionary, any, any> | RequestHandler<ParamsDictionary, any, any>'.
      Type 'RequestHandler<ParamsDictionary, any, any, any>' is not assignable to type 'RequestHandler<ParamsDictionary, any, any>'.
        Type 'RequestHandler<ParamsDictionary, any, any, any>' provides no match for the signature '(req: Request<ParamsDictionary, any, any>, res: Response<any>, next: NextFunction): any'.
  Overload 3 of 3, '(path: PathParams, subApplication: Application): Router', gave the following error.
    Argument of type 'RequestHandler<ParamsDictionary, any, any, any>[]' is not assignable to parameter of type 'Application'.
      Type 'RequestHandler<ParamsDictionary, any, any, any>[]' is missing the following properties from type 'Application': init, defaultConfiguration, engine, set, and 60 more.ts(2769)

or simply

Argument of type 'RequestHandler<ParamsDictionary, any, any, any>[]' is not assignable to parameter of type 'RequestHandler<ParamsDictionary, any, any>'.

my dependencies

"express": "^4.17.1",
"multer": "^1.4.2",
"@types/express": "^4.17.3",
"@types/multer": "^1.4.2",

any solution?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
LinusUcommented, Apr 14, 2020

The Multer typs lives outside of the Express organisation, in DefinitelyTyped. I would recommend opening an issue there instead…

Read more comments on GitHub >

github_iconTop Results From Across the Web

couldn't use multer as router middleware with typescript ...
I use the type any explicitly to accept the middleware without errors as long as this problem is not fixed ... maybe it...
Read more >
Multer handle error, problem with typescript types
I have the following code: import multer from "multer"; const upload = multer(). single('file'); router. post('/add', async (req, res) => { ...
Read more >
How to Upload Files in Node.js using Multer
In a Node.js application, if there is a requirement to upload files to the server Multer is a good option. It can be...
Read more >
How to Migrate Your App from Express to Fastify
Our example Express application only has one router, so we were able to migrate all of the routes in our application to use...
Read more >
body-parser
Node.js body parsing middleware. Latest version: 1.20.1, ... Start using body-parser in your project by running `npm i body-parser`.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found