简体   繁体   中英

Visual Studio Code Intellisense and JavaScript ES5

In Visual Studio Code (VSCode), I'm working with a legacy application that is JavaScript ES5. I've noticed that IntelliSense is not working for all typings that I have installed. Intellisense is working for global typings, but not working for external typings? Since this is legacy code, I rather not change the source .js files to get Intellisense to work.

Intellisense for global typings, such as angular and jquery, seem to work. But, IntelliSense for external typings are not working.

Here you can see that I have four typings installed. Angular and jQuery are installed as globals. LoDash and Ramda are installed as externals. I have a jsconfig.json file created; it's empty. By having the jsconfig.json file, IntelliSense works for the Angular and Jquery, but not for LoDash and Ramda.

VSCode打字

In the next two images, you can see that Intellisense is working for Angular and jQuery.

Angular的VSCode键入

jQuery的VSCode键入

Now, I'm using LoDash, but IntelliSense is not working.

LoDash的VSCode打字

If I import the LoDash library, then IntelliSense works. Since the code is legacy ES5 JavaScript and imports is not supported by ES5, using imports is not a viable option.

带有LoDash和导入的VSCode键入

A good test would be to get IntelliSense working for LoDash. I would like to type "_." and have VSCode display a list of LoDash properties and methods without including any additional code in the source.

Does anyone know how to solve this issue? If so, please provide what worked for you.

Resources

BarDev

As written in the official VS Code JS Intellisense documentation ,

If you are using Visual Studio Code 1.8+, you can alternately explicitly list packages to acquire typings for in your jsconfig.json .

"typeAcquisition": {
    "include": [
        "lodash"
    ]
}

Now when you require or import lodash, Visual Studio Code will use the automatically downloaded typings files for the library to provide rich Intellisense. Most common JavaScript libraries have typings available.

You can create your own type definition file to import things in global context. That will make intellisense to work when you can not use import.

import { AxiosStatic } from "axios";

declare global {
  const axios: AxiosStatic;
}

see also How to get intellisense for external javascript libraries with es5 and Visual Studio Code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM