VS Code: enable javascript intellisense in typescript project

房东的猫 提交于 2021-01-21 06:53:00

问题


Is there a way to enable intellisense for Javascript files in a Typescript project? If I import a function from a javascript package like this:

import foo from "js-package"

and I'm in index.js, I see the intellisense picking up JsDoc comments and listing the parameters taken by the function; if I'm in a .ts file however, I don't get any of this. How do I enable Js intellisense in .ts files, using VS Code?

EDIT: This is what happens:

Ironic, isn't it?


回答1:


You do not need any plugins. Just enable typeAcquisition in your tsconfig.json by adding:

{
    ...
    "typeAcquisition": {
            "enable": true
    }
}

This enables automatic detection and downloading of definition files for typescript. I often reload the window (restart vscode) when I change the settings, to make sure the settings have been read. This is not always necessary though.

This feature is disabled by default if using a tsconfig.json configuration file, but may be set to enabled as outlined further below). source

It was previously under typingOptions.enableAutoDiscovery, but was refactored to typeAcquisition.enable, as shown on this Github issue. You may still find references to on this websites. I find it harder to find information on typeAcquisition, but the schema proves its existence.

Missing tsconfig.json? Create one following the top answer here.




回答2:


Perhaps you have option "typescript.suggest.completeJSDocs": false. In this case, turning it to true could help you.

https://code.visualstudio.com/docs/languages/typescript#_jsdoc-support

Keep in mind that when using JSDoc for TypeScript code, you should not include type annotations. The TypeScript compiler only uses TypeScript type annotations and ignores those from JSDoc.




回答3:


I have this issue with a npm module, which doesn't have type definitions from definitely typed.

Just now I figured out, that if you add a reference to the .js file in your .ts file you get intellisense for the referenced file.

My top of the file looks like this:

///<reference path="../node_modules/tinkerforge/Tinkerforge.js" />
///<reference path="../node_modules/tinkerforge/lib/IPConnection.js" />

import * as tinkerforge from "tinkerforge";

let ip = new tinkerforge.IPConnection();
ip.connect();

It seems like a hack to me, but it does what I want it to do without the hassle of writing d.ts files for this module.

Can anybody else try this and confirm that it works?




回答4:


You no need to initialize the intellisense for typescript it will detect automatically if you want to do explicitly you can check the below image you can select the language from there




回答5:


You might want to enable checkJs in the compiler options. The checkJs options let TypeScript compiler to check types on JavaScript code based on basic type inference and JSDoc.

The other way is delcaring modules by your own: TypeScript has a nice documentation about it.




回答6:


Have you tried "IntelliCode" extension?

Please find the link here: https://marketplace.visualstudio.com/items?itemName=VisualStudioExptTeam.vscodeintellicode



来源:https://stackoverflow.com/questions/50984035/vs-code-enable-javascript-intellisense-in-typescript-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!