Visual Studio Code - TypeScript 使用 Superagent 时的隐式 any 错误
我在一个 TypeScript 项目中使用 superagent,并且已经安装了 @types/superagent,但是看到一个我不理解的类型错误。给出以下情况…
const myRequest = request
.get('/path/to/api')
.end((err, res) => {
// Do stuff with err and res
})
我在 err 和 res 上遇到这些错误:参数 'err' 隐式具有 'any' 类型.ts(7006) 参数 'res' 隐式具有 'any' 类型.ts(7006)
但与此同时,TypeScript 确实似乎知道这些变量的类型,因为当我在 VSCode 中将鼠标悬停在它们上时,它会显示来自 @types/superagent 的正确类型,如下图所示。
在图片中显示,它正确地获取了 res 的类型为 request.Response,来自 @types/superagent。
所以我不明白为什么我会收到这些隐式类型错误。有人能向 TypeScript 新手解释一下吗?谢谢 :)
答案#1
你没有指定参数的类型。请尝试以下方法:
const myRequest = request
.get('/path/to/api')
.end((err, res: request.Response) => {
// Do stuff with err and res
})
