Visual Studio Code Intellisense 不适用于 Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36235242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:48:00  来源:igfitidea点击:

Visual Studio Code Intellisense not working for Javascript

javascriptnode.jsvisual-studio-code

提问by ChrLipp

I am using Visual Studio Code (VSC) 0.10.11 on Windows and Mac. For the purpose of this question I have this small JavaScript snippet:

我在 Windows 和 Mac 上使用 Visual Studio Code (VSC) 0.10.11。出于这个问题的目的,我有这个小的 JavaScript 片段:

'use strict';

const os = require('os');
console.log(os.homedir());

I followed John Papa on Visual Studio Code (Blog entryand Pluralsight Visual Studio Code JavaScript Intellisense- for those who have an account) and therefore I would expect that VSC provides Intellisense and Quick fix options when typings are available.

我在 Visual Studio Code 上关注了 John Papa(博客条目Pluralsight Visual Studio Code JavaScript Intellisense- 对于那些拥有帐户的人),因此我希望 VSC 在输入可用时提供 Intellisense 和快速修复选项。

In the snippet above VSC recognizes consoleand log()(I use hoover, but it is the same with Intellisense):

在上面 VSC 识别console和的片段中log()(我使用 hoover,但它与 Intellisense 相同):

consolelog

安慰日志

but not osand homedir():

但不是oshomedir()

oshomedir

操作系统家目录

But all 4 typings are available in typings/main/ambient/node/index.d.ts. I know that the difference is the requirein the case of os, but in John Papa's video course VSC also provided IntelliSense for required modules. A difference is that John Papa used tsdwhile I am using typings.

但是所有 4 种类型都可以在typings/main/ambient/node/index.d.ts. 我知道,不同的是require在的情况下os,但在约翰爸爸的视频当然也VSC所需模块提供智能感知。不同之处在于 John Papatsd在我使用typings.

So my questions are

所以我的问题是

  • how can I enable Intellisense for all known typings?
  • what do I have to do that VSC offers me Quick fix (green line under moduls with missing typings)?
  • 如何为所有已知类型启用智能感知?
  • 我该怎么做才能让 VSC 为我提供快速修复(缺少类型的模块下的绿线)?

采纳答案by Dauren Akilbekov

The above links are outdated. In older versions of VS Codeyou needed to reference your typings like /// <reference path> for somelibrary.d.ts.

以上链接已过时。在旧版本的VS Code 中,您需要引用像/// <reference path> for somelibrary.d.ts.

With new version you need to initialize your project by creating jsconfig.jsonat the root of your project and add the following inside:

对于新版本,您需要通过jsconfig.json在项目的根目录下创建并在其中添加以下内容来初始化您的项目:

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs"
    },
    "exclude": [
      "node_modules"
    ]
}

Next install typing you need. You can use either tsd or typings. In your case you need to install tsd install nodeor typings install node --ambient. Make sure you have typings/tsdinstalled. Restart project.

接下来安装你需要的输入。您可以使用 tsd 或typings。在您的情况下,您需要安装tsd install nodetypings install node --ambient. 确保您已typings/tsd安装。重启项目。

Please refer to docs:

请参考文档:

  1. Setup JS project - https://code.visualstudio.com/docs/languages/javascript
  2. Node.js - https://code.visualstudio.com/docs/runtimes/nodejs
  3. Debugging - https://code.visualstudio.com/docs/editor/debugging
  1. 设置 JS 项目 - https://code.visualstudio.com/docs/languages/javascript
  2. Node.js - https://code.visualstudio.com/docs/runtimes/nodejs
  3. 调试 - https://code.visualstudio.com/docs/editor/debugging

Update:

更新:

Since version 1.7 there is no need to manually install typings, they should be downloaded automatically. Better JavaScript IntelliSense

由于版本 1.7 不需要手动安装类型,它们应该自动下载。更好的 JavaScript 智能感知

回答by Lord

Well, after 4 hr's googling finally, I decided to uninstall nodejs, npm, and typescript then install all of them again. The previous time I installed them using nvmbut this time I decided not to use nvm just install them from node source since I am using Ubuntu I executed bellow commands, for windows or mac just install them without any package or version manager.

好吧,经过 4 小时的谷歌搜索,我决定卸载 nodejs、npm 和 typescript,然后重新安装所有这些。上一次我使用nvm安装它们,但这次我决定不使用 nvm 只是从节点源安装它们,因为我使用的是 Ubuntu 我执行了波纹管命令,对于 windows 或 mac 只需安装它们,无需任何包或版本管理器。

curl https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key add -
sudo apt-add-repository "deb https://deb.nodesource.com/node_7.x $(lsb_release -sc) main"
sudo apt-get update
sudo apt-get install nodejs

above command installed both nodejs and npm, after then to install typescript I ran bellow command

上面的命令安装了 nodejs 和 npm,然后安装打字稿我运行了下面的命令

sudo npm install --global typescript

I updated my VSCode to the newest version.

我将我的 VSCode 更新到最新版本。

enter image description here

在此处输入图片说明

Then in the bottom right of my VSCode I clicked on javascript to change the language mode, I wrote 'type' on the search bar and select typescript as my new selected language mode...........BINGO

然后在我的 VSCode 的右下角我点击 javascript 来更改语言模式,我在搜索栏上写了“类型”并选择打字稿作为我新选择的语言模式.........宾果

enter image description here

在此处输入图片说明