3
  1. npm init -y
  2. npm i axios
  3. npm i @types/axios --save-dev

How come VS code 1.62 doesn't appear to give the schema of the response object when I am typing out code like:

resp = await axios("https://httpstat.us/404");
resp. 

<C-Space> shows confusing / inappropriate completions.

What am I missing please? I tried renaming the file to typescript and that doesn't help either, so I am really confused how to edit Javascript effectively. I.e. for the editor to diagnose that I am using the wrong properties and prompt for using the right ones with documentation.

hendry
  • 9,725
  • 18
  • 81
  • 139

1 Answers1

0

Name the file with a .ts extension and import axios with:

import axios from 'axios'

Then the following should work with full autocompletion:

async function testing() {
  const resp = await axios("https://httpstat.us/404");
  resp.status // works
}

Typescript doesn't always get the types right with the require function. import is the right way you do this in typescript.

See the docs

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • But with imports, you can't run it? `SyntaxError: Cannot use import statement outside a module` – hendry Nov 07 '21 at 07:57
  • Try adding `export` to something in that file. Also see https://stackoverflow.com/questions/58273824/typescript-cannot-use-import-statement-outside-a-module – Alex Wayne Nov 07 '21 at 21:57