严格模式下的 tsconfig 问题和 Type (...) 不可分配给 type (...)

Issue with tsconfig in strict mode and Type (...) is not assignable to type (...)

我知道有很多关于这个的问题,我确实阅读了其中的大部分(我猜),它们帮助我更好地理解了这个错误,以及在大多数情况下如何解决它。

但是我面临一个我自己无法解决的问题,所以如果 SO 不是这个问题的好地方,我深表歉意(我想这可能是一个骗局,因为已经有关于这个问题的问题,但我没有找到任何帮助我解决这个问题的方法 "particular case").

我有以下代码:

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()

有了这个 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"]
}

但是当运行 ./node_modules/typescript/bin/tsc 我有以下错误:

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'

所以我猜 ): void => { 行是错误的,因此我尝试了很多东西来调试它但没有找到任何成功的东西。

./node_modules/typescript/bin/tsc -v的输出是Version 3.3.4000

欢迎任何帮助,如果我忘记了一些相关信息,请告诉我

因为这个错误在 node_modules 中,修复它的唯一希望是对 typings 存储库的拉取请求。

现在让错误消失 "skipLibCheck": tsconfig 中的 true。

编辑:抱歉误读了错误的来源。

"Types of parameters '_req' and 'req' are incompatible." 我怀疑你是否使用 express 你最终得到了两种类型 "Request"表达用途。