VSCode TypeScript Intellisense not working

2019-06-10 15:47发布

问题:

I am pulling my hair out here, literally. VSCode 1.6.0 on Mac, Typescript 2.0.2, but I have also tried 2.0.0.

I've tried targeting es5, es6, with or without commonjs module targeting. I can't get intellisense for other files local to my project nor anything in the node_modules folder. I even installed typescript files from node_modules into my typings folder and those don't work.

The only intellisense I get is for my globally installed typings.

{
    "compilerOptions": {
        "target": "es6",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "allowSyntheticDefaultImports": true,
        "moduleResolution": "node"
    },
    "exclude": [
        "node_modules"
      ]
}

回答1:

After lots of trial and error, I finally got Node, Typescript, VSCode (and Visual Studio for that matter), and IntelliSense working correctly. This also appears to work with WebStorm.

Create a new Typescript project from the command prompt:

  1. mkdir newproject (or any directory name you want)
  2. cd newproject
  3. npm init -f (this will create a package.json)
  4. npm install @types/node --save-dev (this will create a nodes_module folder and a package-lock.json file)
  5. Add an initial tsconfig.json file. It could be as simple as {"files" : ["main.ts"] }
  6. Add an initial empty main.ts file to get started

Now launch VS Code point it to open the folder created in the steps above.

Most important. Use the import keyword when declaring module use. Otherwise, IntelliSense flat out won't work. It took me quite a while a lot of head banging to realize this was the missing step.

Instead of this:

var http = require("http");

Type this:

import http = require("http");

And magically, you've got a barebones Typescript + Node project with IntelliSense working.