3

I am using superagent in a TypeScript project and have installed @types/superagent but am seeing a type error I don't understand. Given the following...

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

I get these errors for err and res: Parameter 'err' implicitly has an 'any' type.ts(7006) Parameter 'res' implicitly has an 'any' type.ts(7006)

But at the same time TypeScript does seem to know the types of these variables because when I hover on them in VSCode it shows the correct types from @types/superagent, as can be seen in the below image.

VSCode hover

In the image it shows that it is correctly getting the type of res as request.Response from @types/superagent.

Therefore I don't understand why I am getting these implicit type errors. Is anybody able to explain this to a TypeScript newbie? Thanks :)

Michael
  • 1,643
  • 1
  • 15
  • 31
  • 1
    Actually that's not a newbie thing. It's truly bizarre if I understand you correctly, because it's simultaneously telling you it knows the type and doesn't know the type. Just to confirm, that screenshot is all one piece, right? Not assembled from different places? Also, what do you see if you hover over the `.end`? – user2740650 Feb 14 '20 at 00:08
  • 1
    Note that VSCode has its own built-in version of TypeScript which it uses to give that inline type info. By default, it's different from the one you have on disk. Have you tried restarting VSCode? That will force it to rescan all the imports for type info. – user2740650 Feb 14 '20 at 00:11
  • i do believe that you have set 'res:requset.Response' somewhere in your code – A Farmanbar Feb 14 '20 at 00:15
  • 2
    @Mr.AF https://www.typescriptlang.org/play/#code/GYVwdgxgLglg9mABBAhgGzQUwE4ApgBciuKRAzlNjGAOYA0iARkWCALaM4CUiAvAHyIAbnBgATHgG9EAXwBQc1Bhy5cALwYAPHgMTSZXANxA typescript should be able to infer it, no need to repeat it twice. – zerkms Feb 14 '20 at 00:16
  • @zerkms yea it did and show error right now. – A Farmanbar Feb 14 '20 at 00:17
  • @Mr.AF it should not have, see my example. – zerkms Feb 14 '20 at 00:25
  • @zerkms i got yea , you are right , if parameter set to any there should be not any error right? – A Farmanbar Feb 14 '20 at 00:27
  • 1
    @Mr.AF If you explicitly type an argument of a callback as `any` it still would pass since those 2 function types would be still compatible. – zerkms Feb 14 '20 at 00:28
  • @user2740650 Yes the screenshot is all one piece. I have tried restarting VSCode but the errors persist. I also get the errors if I run TypeScript from the command line. When hovering on `.end` it shows the correct info extracted from `@types/superagent`. Very weird. – Michael Feb 17 '20 at 21:49
  • @Michael I've never seen this before, but I use TypeScript all the time. Are you able to set up a simple demo link somewhere (like maybe https://jsfiddle.net/)? That way we can see it in action. – user2740650 Feb 19 '20 at 13:06
  • I am also getting these errors on Bitbucket / Jenkins, despite the fact that VS Code knows the type of the parameter. I wondered if it was caused by overzealous linting. I fixed it by explicitly adding the type, but I'd like to know why it is doing it. – Yvonne Aburrow Oct 22 '20 at 14:53

1 Answers1

-2

You are not specifying the type of the parameters. Try the following:

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