Typescript tsconfig在严格模式下出现问题,类型(…)不可分配给类型(…)
我知道关于这一点有很多问题,我确实读了其中的大部分,我想他们帮助我更好地理解这个错误,以及在大多数情况下如何解决它 然而,我面临一个我自己无法解决的问题,因此,如果这样做不是解决这个问题的好地方,我想这可能是一个骗局,因为已经有关于这个问题的问题,但我没有找到任何有助于我解决这个特殊案件的方法 我有以下代码:Typescript tsconfig在严格模式下出现问题,类型(…)不可分配给类型(…),typescript,types,multer,tsconfig,Typescript,Types,Multer,Tsconfig,我知道关于这一点有很多问题,我确实读了其中的大部分,我想他们帮助我更好地理解这个错误,以及在大多数情况下如何解决它 然而,我面临一个我自己无法解决的问题,因此,如果这样做不是解决这个问题的好地方,我想这可能是一个骗局,因为已经有关于这个问题的问题,但我没有找到任何有助于我解决这个特殊案件的方法 我有以下代码: import { Request } from 'express' import multer from 'multer' import path from 'path' class I
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的输出版本为3.3.4000
欢迎提供任何帮助,如果我忘记了一些相关信息,请告诉我,因为此错误在node_模块中。修复此错误的唯一希望是向typings存储库发出请求 现在要消除错误,请在tsconfig中打开skipLibCheck:true 编辑:很抱歉误读了错误的来源 参数“_req”和“req”的类型不兼容。
我怀疑如果您使用的是express,那么您最终会得到两种类型的请求:一种是在标准typescript库中键入的1个函数,另一种是express使用的express中存在的请求类型。好吧,我可能弄错了,但我不认为错误来自我没有发布上一个版本的node_模块f tsconfig.json,抱歉,但我有:exclude:[**/spec/*.spec.ts,./node\u modules]我将在问题中对此进行编辑。此外,错误显然出现在我的文件服务/middleware/image/image.middleware.ts的第112行。我认为这只是typescript的一个很好的提示,告诉我要检查哪个定义文件。该文件位于节点模块中,因此它指向该文件。我尝试了skipLibCheck:true tho,但输出仍然是相同的。抱歉一定是误读了错误的来源,我还将编辑node_modules错误稍微缩进的事实对我来说也似乎是typescrypt打印了一些stacktrace来帮助我,遗憾的是,我还没有找到如何为这个特定的定义放置正确的类型:谢谢你的帮助:不确定如何加载正确的模块e tho.I加载导入{Request}如果multer正在使用另一种类型的请求,我如何加载它?我尝试了类似于_req:express.multer的方法。但是它只自动完成express.multer.file。我不熟悉multer,但请求肯定是这里的问题。就签名而言,这两种类型的请求的类型是不同的。