4
votes

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

2

2 Answers

13
votes

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;
 }