Multer: couldn't use multer as router middleware with typescript

Created on 14 Apr 2020  路  5Comments  路  Source: expressjs/multer

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?

Most helpful comment

All 5 comments

my temporary solution is to switch the file to js 馃ぃ

Could you add the code for someEndpoint?

sure

import { Request, Response, NextFunction } from "express";

const someEndpoint = (
  req: Request,
  res: Response,
  next: NextFunction,
) => {
  try {
    console.log(req.files);
    res.json({
      message: "you're trying to upload some files.",
    });
  } catch (err) {
    res.status(err.status || 500).send({
      message: err.message || "Something went wrong",
      error: err,
    });
  }
};

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

Was this page helpful?
0 / 5 - 0 ratings