我在一个TypeScript项目中使用superagent,并且已经安装了@types/superagent,但是我看到一个我不理解的类型错误。考虑到以下几点...
const myRequest = request
.get('/path/to/api')
.end((err, res) => {
// Do stuff with err and res
})对于err和res,我得到了以下错误:Parameter 'err' implicitly has an 'any' type.ts(7006) Parameter 'res' implicitly has an 'any' type.ts(7006)
但与此同时,TypeScript似乎确实知道这些变量的类型,因为当我在VSCode中将鼠标悬停在这些变量上时,它会显示来自@types/superagent的正确类型,如下图所示。

在该图中,它显示它正确地从@types/superagent获取了res的类型为request.Response。
因此,我不明白为什么我会得到这些隐式类型错误。有人能向TypeScript新手解释这一点吗?谢谢:)
发布于 2020-02-14 07:56:36
您没有指定参数的类型。尝试以下操作:
const myRequest = request
.get('/path/to/api')
.end((err, res: request.Response) => {
// Do stuff with err and res
})https://stackoverflow.com/questions/60218070
复制相似问题