uisng multer with typescript: Property 'file' does not exist on type 'Request'.ts(2339)
Asked Answered
I

7

19

I am using https://www.npmjs.com/package/multer library and my node.js app is written in typescript.

I get the following typescript error in my code.

Property 'file' does not exist on type 'Request'.ts(2339)

    public document = async (req: Request, res: Response): Promise<any> => {
        const documentFile = req.file;
    }

How can i resolve this. The req is the express Request object but multers middleware appends a .file into this request object. However it is not aware of this because the types request interface does not originally contain a file properly

Interdenominational answered 15/10, 2019 at 11:27 Comment(0)
C
24

Probably we can just extend Request

interface MulterRequest extends Request {
    file: any;
}

 public document = async (req: Request, res: Response): Promise<any> => {
   const documentFile  = (req as MulterRequest).file;
 }

or may be like this code

interface MulterRequest extends Request {
    file: any;
}

 public document = async (req: MulterRequest , res: Response): Promise<any> => {
   const documentFile  = req.file;
 }
Cherellecheremis answered 15/10, 2019 at 12:35 Comment(3)
i went with something simple like const documentFile = (req as any).file;Interdenominational
@daliusd's comment below should be the accepted answer. npm i @types/multer --save-dev is all that is required.Brady
I am getting following Error : Types of parameters 'req' and 'req' are incompatible. Type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>' is not assignable to type 'MulterRequest'. Property 'file' is optional in type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>' but required in type 'MulterRequest'.ts(2769)Thyratron
T
24

Or install @types/multer with

npm i @types/multer --save-dev

and check out this https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18569

Telic answered 2/2, 2021 at 23:43 Comment(1)
Can you elaborate on how to solve the problem listed here? If you can provide solution using the link you attached, that would be more beneficial to people seeking an answer to the listed problem.Columbous
B
6

Like @daliusd mentioned in his answer, the accepted answer here should be:

npm i @types/multer --save-dev

req.files is now typed and recognized by the Typescript compiler.

Brady answered 15/11, 2022 at 12:55 Comment(0)
V
1

if using typescript with express-fileupload you can install @types/express-fileupload as dev dependencie and it will be solved

Vizier answered 9/12, 2022 at 7:59 Comment(0)
B
0

For me I was not importing the correct Request, Response objects.

Adding the correct imports fixed it.

import { Request, Response, NextFunction } from 'express';
Bangup answered 24/10, 2023 at 16:34 Comment(0)
L
0

npm i -D @types/multer worked for me

Lotz answered 8/5, 2024 at 4:57 Comment(0)
L
0

For me worked:

  1. install npm i @types/multer --save-dev;
  2. Use file multer type from express at controller method param. Like this: async uploadUserImage(req: Request & Express.Multer.File, res: Response, next: NextFunction) {
Latricelatricia answered 1/6, 2024 at 18:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.