I know there are quite a lot of question about this, I did read most of them (I guess) and they helped me having a better understanding of this error, and how to solve it in most cases.
However I am facing one I could not resolves by myself, so apologies if SO is not the good place for this question (I guess this could be a dupe since there is already questions about this issue, but I did not found any that helped me to solve this "particular case").
I have the following piece of code :
import { Request } from 'express'
import multer from 'multer'
import path from 'path'
class ImageMiddleware {
// ...
private storage (folder: string): multer.StorageEngine {
return multer.diskStorage({
destination: (
_req: Request,
_file: Express.Multer.File,
callback: (error: Error | null, destination: string) => void
): void => {
return callback(null, `./public/img/${folder}`)
},
filename: (
req: Request,
file: Express.Multer.File,
callback: (error: Error | null, destination: string) => void): void => {
callback(
null,
`${file.fieldname}`
+ `-`
+ `${Date.now()}`
+ `${req.user.id}`
+ `${path.extname(file.originalname)}`
)
},
})
}
// ...
}
export default new ImageMiddleware()
With this tsconfig.json :
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"outDir": "./dist/",
"sourceMap": true,
"removeComments": true,
"pretty": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"alwaysStrict": true,
"noImplicitThis": true,
"noImplicitAny": true,
"allowUnreachableCode": false,
"esModuleInterop": true,
"strict": true
},
"exclude": ["**/spec/*.spec.ts", "./node_modules"]
}
But when running ./node_modules/typescript/bin/tsc
I have the following error :
services/middlewares/image/image.middleware.ts:112:13 - error TS2322: Type '(_req: Request, _file: File, callback: (error: Error | null, destination: string) => void) => void' is not assignable to type 'string | ((req: Request, file: File, callback: (error: Error | null, destination: string) => void) => void) | undefined'.
Type '(_req: Request, _file: File, callback: (error: Error | null, destination: string) => void) => void' is not assignable to type '(req: Request, file: File, callback: (error: Error | null, destination: string) => void) => void'.
Types of parameters '_req' and 'req' are incompatible.
Type 'Request' is missing the following properties from type 'Request': get, header, accepts, acceptsCharsets, and 71 more.
112 destination: (
~~~~~~~~~~~
node_modules/@types/multer/index.d.ts:59:9
59 destination?: string | ((req: Express.Request, file: Express.Multer.File, callback: (error: Error | null, destination: string) => void) => void);
~~~~~~~~~~~
The expected type comes from property 'destination' which is declared here on type 'DiskStorageOptions'
So I guess that the ): void => {
line is wrong, hence I tried a lot of stuff to debug it but did not find anything successfull.
The output of ./node_modules/typescript/bin/tsc -v
is Version 3.3.4000
Any help would be welcome, please let me know if I forgot some relevant informations
Because this error is in node_modules the only hope of fixing it is a pull request to the typings repository.
For now to silence the error turn "skipLibCheck": true in tsconfig.
EDIT: Sorry misread where error was coming from.
"Types of parameters '_req' and 'req' are incompatible." I Suspect if you're using express you've ended up with two types for "Request" there is one in the standard typescript library which 1 function is typed with and another type for "Request" that exists in express that express uses.