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.

3
  • Does this help ? code.visualstudio.com/docs/editor/intellisense#_troubleshooting or try create local variable instead of using global variables. const resp = ... Commented Nov 7, 2021 at 5:15
  • no, it doesn't really help. require() = no sane completion Commented Nov 7, 2021 at 6:37
  • Visit that URL in a browser... Commented Nov 7, 2021 at 7:18

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

But with imports, you can't run it? SyntaxError: Cannot use import statement outside a module
Try adding export to something in that file. Also see stackoverflow.com/questions/58273824/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.