TypeScript 隐含了 Superagent 的任何错误

标签 typescript visual-studio-code superagent

我在 TypeScript 项目中使用 superagent 并安装了 @types/superagent 但我看到一个我不明白的类型错误。鉴于以下...

const myRequest = request
  .get('/path/to/api')
  .end((err, res) => {
    // Do stuff with err and res
  })

我得到了 errres 的这些错误: 参数“err”隐式具有“any”类型.ts(7006) 参数“res”隐式具有“any”类型.ts(7006)

但同时 TypeScript 似乎确实知道这些变量的类型,因为当我在 VSCode 中将鼠标悬停在它们上面时,它会显示来自 @types/superagent 的正确类型,如在下图。

VSCode hover

在图像中它显示它正确地从 @types/superagent 获取 res 的类型作为 request.Response

因此我不明白为什么会出现这些隐式类型错误。有人能向 TypeScript 新手解释一下吗?谢谢:)

最佳答案

您没有指定参数的类型。尝试以下操作:

const myRequest = request
  .get('/path/to/api')
  .end((err, res: request.Response) => {
    // Do stuff with err and res
  })

关于TypeScript 隐含了 Superagent 的任何错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60218070/

相关文章:

javascript - 如何等待在 map 内解决的 promise ?

typescript - 参数中的泛型和typeof T

typescript - 修改 VS Code 扩展中现有的 CodeLens

javascript - 使用 Jest 对 redux 异步函数进行单元测试

javascript - 使用 ReactJS、Superagent 和 Python (flask) 进行 POSTing

javascript - React + Flux, ES6, Babel ActionCreate using json-server and super agent, 数据不响应

javascript - 正则表达式 - 如何匹配逗号之间的文本

visual-studio-code - 如何在VS代码中为Latex修改makeindex脚本

regex - VSCode 代码段的多重转换

typescript - 解构歧视联盟