Visual Studio 2017,JavaScript智能感知不一致 [英] Visual Studio 2017, JavaScript intellisense inconsistencies

查看:100
本文介绍了Visual Studio 2017,JavaScript智能感知不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR :如何在Visual Studio 2017中包含tsconfig.json文件,并且仍像默认情况下一样具有JavaScript intellisense工作,为您自己的代码和3rd-政党图书馆?是不是很烦,还是我只需要对它的工作方式有更好的了解?

与jQuery一样,我希望通过智能感知在JavaScript和html文件中普遍访问我自己的JavaScript名称空间.如何做到这一点?


编辑3 :
我在



如果我对Visual Studio中的JavaScript智能感知的期望不正确,以下是我期望它如何运行的一个精心设计的示例.

假设我在./Scripts文件夹中创建了三个TypeScript文件,我们将它们称为 A.ts B.ts C.ts .每个都将包含其自己的名称空间:

A.ts

namespace A {
    export function f1(s: string) {
        return s;
    }
}

B.ts

namespace B {
    export function f1(n: number) {
        return n;
    }
}

C.ts

namespace C {
    export function f1(b: boolean) {
        return b;
    }
}

在这一点上,人们可能希望能够开始看到其新创建的命名空间的intellisense,实际上,在A.ts,B.ts和C.ts中,您开始看到这些命名空间的intellisense.很好.

不幸的是,这不会延续到其他地方,例如/Home/Index.cshtml. Index.cshtml对A,B或C名称空间一无所知.此外,.js文件对这些名称空间一无所知,因此需要它们各自的TypeScript声明(.d.ts)文件.

因此,我们继续将tsconfig.json文件添加到项目的根目录,并像上面一样配置它.如果我们对其中一个.ts文件执行编辑并将其保存,则会触发将相应的.d.ts文件编译到./Scripts/out文件夹中.现在,我们是否在Index.cshtml和.js文件中获得了智能感知功能? ...不.

现在,您可能会认为这些文件需要包含在项目中,以便Visual Studio能够进行智能识别(创建时将它们排除在外).因此,您将它们包括在项目中. Intellisense?否.jQueryintellisense?否.也许它们需要与要使用它们的文件位于同一目录中? .....有点儿?在很多情况下,我都遇到了这样的intellisense不一致:

以某种方式,我们在C3.js(注:一个JavaScript文件)中获得了对"A"命名空间的智能感知,但没有其他的.什么?

也许您认为自己在正确的道路上,并且在智能感知引擎方面取得了小小的胜利.但是然后您重新启动Visual Studio实例并...

等等....我 just 对'A'命名空间具有智能感知.发生什么事了?

我不知道.但是在弄乱了一点之后,我尝试在.js文件的顶部放置一个import语句,突然之间,所有的intellisense都开始为导入的文件/模块提供帮助.众议院!

甚至在Index.cshtml中,我也开始为导入的命名空间接收智能感知.

但是,当我使用import语句关闭.js文件时,突然之间,我突然失去了Index.cshtml中对它们的智能感知.等等...什么?而且与JavaScript文件不同,我无法编写导入语句来接收它们的智能感知:

或者至少……不是我自己的命名空间:

这种前后不一致的结果使我对Visual Studio的JavaScript智能感知的实际工作方式感到困惑.我仍然不知道它是否已损坏,或者是否需要更好地了解它.


原始帖子

系统
Windows 10
VS2017 15.0.0 + 26228.10
启用了新的JavaScript语言服务

我一直在努力让Visual Studio 2017的JavaScript intellisense可以为我工作几天,但无济于事.我不知道在IDE中是否真的有些古怪,或者我只是不太了解它.

我对 VS2017的JS intellisense工作方式的理解现在是通过打字稿定义文件或.d.ts文件.最终,我希望获得intellisense的帮助,以我们自己的TypeScript/JavaScript来帮助我们,但现在我很乐意让它在一个新项目中始终如一地工作.

如果我创建一个新的ASP.NET MVC 4.5.2项目,则开箱即用的JS intellisense似乎可以正常工作,并且我的意思是我可以在.js中键入一个美元符号($).文件或< script >标记之间,我可以正确接收jQuery的intellisense.惊人的.现在,考虑到创建TypeScript文件(.ts)的最终目标,并让TypeScript编译器自动生成.d.ts文件,以便我们可以为自己的代码获得丰富的智能感知,我希望介绍一个tsconfig.json文件到项目的根目录以进行配置.

创建tsconfig.json文件后,JS intellisense不再起作用.我在.js文件中或< script >标记之间键入一个美元符号($),对jQuery的智能感知为零.这是一段令人沮丧的经历,但是我读了上面链接页面上的这份小笔记:

注意:如果使用tsconfig.json,则默认情况下禁用此功能 配置文件,但可以将其设置为启用,如进一步概述 下面).

此功能"是指默认情况下,Salsa语言服务将尝试检测正在使用的JavaScript库,以便提供更丰富的IntelliSense."好的,所以Salsa语言服务将自动检测正在使用的库,并在存在tsconfig.json文件时为它们提供智能感知,除非您将其特别配置为在下面概述".至少我是这样理解的.

如果我们稍微向下滚动页面进入tsconfig文件设置,则会遇到 enableAutoDiscovery 的设置,该设置启用如上所述的自动检测和下载定义文件".关于如上所述"的含义并不是很明确,但是我只能假定它是参考先前的说明,该说明重新启用了Salsa的自动检测功能以提供丰富的智能感知功能.所以我在tsconfig.json文件中包含了该选项,并且...没有好处;仍然没有JS intellisense.

事实证明, typingOptions.enableAutoDiscovery 已重命名为 typeAcquisition.enable . rr好吧,所以我们在tsconfig.json aaaaaa中进行了更改,...仍然没有JS intellisense.

在这一点上,我 am 能够编写自己的.d.ts文件,Visual Studio的intellisense可以正常运行(大部分时间...),但是我仍然没有intellisense对于像jQuery这样的第三方库来说,这是不好的.

到目前为止,我已经进入了以下tsconfig.json文件:

{
  "compilerOptions": {
    "noEmitOnError": true,
    "noImplicitAny": false,
    "removeComments": false,
    "target": "es5",
    "declaration": true,
    "declarationDir": "./Scripts/out"
  },
  "compileOnSave": true,
  "typeAcquisition": {
    "enable": true, 
    "include": ["jquery"]
  },
  "include": [
    "./Scripts/app/**/*"
  ],
  "exclude": [
    "./Scripts/vendors/**/*",
    "./Scripts/out/**/*"
  ]
}

在看到TypeScript虚拟项目的情况下,我可以看到我的项目的.ts文件以及jQuery都包含在我的%LOCALAPPDATA%\ Microsoft \ TypeScript \ node_modules \ @types目录中.

我希望所有这些都能够对我自己项目的.ts文件和3rd-party库(至少包括那些具体包括在内的jQuery都具有intellisense). las ...不.仍然没有jQuery intellisense.

顺便说一句,如果我为jQuery编写了一个导入行,突然之间我得到了jQuery intellisense

...有趣.也许这很合理;我并没有对模块导入以及Visual Studio的intellisense如何处理这些问题一无所知,但是我真正想要的是在我们的代码库和第三方库中都具有应用程序级的intellisense. >

解决方案

似乎是最新更新(2017年3月14日(内部版本26228.09))引起了此问题,您可以转到控制面板-程序和功能-查看已安装的更新和删除此更新,然后重新打开VS以检查智能感知.如果有效,则意味着此更新导致了此问题,您暂时无法安装此更新

我注意到Visual Studio Extension的Microsoft Report Projects在项目中使用时可能会引起诸如此类的问题.在将SQL Server Report项目添加到解决方案并重新启动Visual Studio之后,我的一些故障了..因此,目前我从解决方案中删除了我的Report项目,但我希望Microsoft可以解决此问题

TL;DR: How do you include a tsconfig.json file in Visual Studio 2017 and still have JavaScript intellisense work like it does by default, giving code hints both for your own code and 3rd-party libraries? Is it borked, or do I just need a better understanding of how it works?

Much like jQuery, I would like ubiquitous access to my own JavaScript namespaces across JavaScript and html files with intellisense. How does one achieve this?


EDIT 3:
I reported this issue on developercommunity.visualstudio.com. Please consider up-voting the issue to attract more attention from Microsoft.


EDIT 2:
Currently the "solution" is Resharper, which provides what one would expect straight out-of-the-box. But it's not really ideal at a price of $299/yr/user for something Visual Studio markets it provides itself.


EDIT 1:
In the event that my expectations for JavaScript intellisense in Visual Studio is inaccurate, below is a contrived example of how I expect it to function.

Say I create three TypeScript files in my ./Scripts folder, we'll call them A.ts, B.ts, and C.ts. Each will contain its own namespace:

A.ts

namespace A {
    export function f1(s: string) {
        return s;
    }
}

B.ts

namespace B {
    export function f1(n: number) {
        return n;
    }
}

C.ts

namespace C {
    export function f1(b: boolean) {
        return b;
    }
}

At this point in time one may expect to be able to start seeing intellisense for their newly created namespaces, and indeed, within A.ts, B.ts, and C.ts you begin to see intellisense for those namespaces. Cool.

Unfortunately this doesn't carry over to other places, like /Home/Index.cshtml. Index.cshtml knows nothing about the A, B, or C namespaces. Additionally, .js files know nothing about these namespaces, hence the need for their respective TypeScript Declaration (.d.ts) files.

So we'll go ahead and add our tsconfig.json file to the root of our project and configure it like we do above. If we perform an edit on one of our .ts files and save it, this triggers the compilation of our corresponding .d.ts files into our ./Scripts/out folder. Do we now get intellisense in Index.cshtml and .js files? ...No.

Now you might be thinking these files need to be included in the project in order for Visual Studio to pick them up for intellisense (they're excluded on creation). So you include them in the project. Intellisense? No. jQuery intellisense? No. Perhaps they need to be in the same directory as the file you want to use them in? .....Kind of? I've ran into intellisense being inconsistent like this on a number of occassions:

Somehow we're getting intellisense for our 'A' namespace within our C3.js (note: a JavaScript file), but none of the other ones. What?

Perhaps you think you're on the right track and that you've scored a small victory against the intellisense engine. But then you restart your Visual Studio instance and...

Wait....I just had intellisense for the 'A' namespace. What happened!?

I have no idea. But after messing around for a bit I try placing an import statement at the top of the .js file, and all of a sudden intellisense starts kicking in for the imported files/modules. Huzzah!

And even in Index.cshtml I begin receiving intellisense for our imported namespaces.

The moment I close the .js file with the import statements, though, all of a sudden I lose the intellisense for them in Index.cshtml. Wait...what?? And unlike in JavaScript files, I'm unable to write an import statement to receive intellisense for them:

Or at least... not with my own namespaces:

It's this inconsistent back-and-forth that confuses the heck out of me as to how exactly Visual Studio's JavaScript intellisense actually works. I still don't know if it's just broken or if I need a better understanding of it.


ORIGINAL POST

System
Windows 10
VS2017 15.0.0+26228.10
New JavaScript language service is enabled

I've been fighting to get Visual Studio 2017's JavaScript intellisense to work for me for a couple of days now to no avail. I don't know if something's genuinely wonky with the IDE or if I just don't understand it properly.

My understanding of how VS2017's JS intellisense works is now by typescript definition files, or .d.ts files. Ultimately I would like to get intellisense to help us out with our own TypeScript/JavaScript but right now I'd settle for just having it work consistently within a fresh project.

If I create a new ASP.NET MVC 4.5.2 project, out-of-the-box JS intellisense seems to work fine, and by that I mean I can type a dollar sign ($) either in a .js file or between <script> tags and I properly receive intellisense for jQuery. Awesome. Now, with the ultimate goal in mind of creating TypeScript files (.ts) and having the TypeScript compiler generate our .d.ts files automagically so that we can get rich intellisense for our own code, I wish to introduce a tsconfig.json file to the root of the project in order to configure that.

After creating the tsconfig.json file JS intellisense ceases to function. I type a dollar sign ($) into either a .js file or between <script> tags and I get zero intellisense for jQuery. This was a frustrating experience for a while but then I read this little note found on the linked page above:

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

"This feature" is in reference to "By default, the Salsa language service will try to detect which JavaScript libraries are in use...in order to provide richer IntelliSense." Ok, so the Salsa language service will not autodetect libraries in use and provide intellisense for them when a tsconfig.json file is present, unless you specifically configure it to do so as "outlined below." At least, that's how I understand it.

If we scroll down the page a little bit and come to the tsconfig file settings we come across a setting for enableAutoDiscovery which "enables the automatic detection and download of definition files as outlined above." It's not super explicit as to what "as outlined above" is in reference to, but I can only assume it's in reference to the previous note indicative of re-enabling Salsa's auto-detection feature to provide rich intellisense. So I include that option in my tsconfig.json file and...no benefit; still no JS intellisense.

As it turns out, typingOptions.enableAutoDiscovery has been renamed to typeAcquisition.enable. Grrrrr. Alright, so we make that change in our tsconfig.json aaaaaaand... still no JS intellisense.

At this point I am able to write my own .d.ts files and Visual Studio's intellisense will pick it up alright (most of the time...), but I still have no intellisense for 3rd-party libraries like jQuery, which is no good.

So far I've landed on the following tsconfig.json file:

{
  "compilerOptions": {
    "noEmitOnError": true,
    "noImplicitAny": false,
    "removeComments": false,
    "target": "es5",
    "declaration": true,
    "declarationDir": "./Scripts/out"
  },
  "compileOnSave": true,
  "typeAcquisition": {
    "enable": true, 
    "include": ["jquery"]
  },
  "include": [
    "./Scripts/app/**/*"
  ],
  "exclude": [
    "./Scripts/vendors/**/*",
    "./Scripts/out/**/*"
  ]
}

With the TypeScript virtual project in view I can see that my project's .ts files are included as well as jQuery in my %LOCALAPPDATA%\Microsoft\TypeScript\node_modules\@types directory.

I was hopeful that with all this in place I would have both intellisense for my own project's .ts files and 3rd-party libraries (at least those specifically included; jQuery only at this point). Alas...no. Still no jQuery intellisense.

Incidentally, if I write an import line for jQuery, all of a sudden I get jQuery intellisense

...interesting. And maybe that makes perfect sense; I haven't messed a whole lot with module imports and how Visual Studio's intellisense handles those, but what I really want is to have intellisense application-wide for both our code and 3rd-party libraries.

解决方案

It looks like the latest update March 14, 2017 (build 26228.09) caused this issue, you can go to Control Panel—Programs and Features-View Installed updates and remove this update, then reopen VS to check the intellisense. If it works, which means this update lead to this issue, you can temporarily not install this updat

I've noticed the Microsoft Report Projects for Visual Studio Extension can cause problems such as this when being used in a project, A couple of mine have broken right after I add my SQL Server Report project to the solution and restarted visual studio.. So for now I remove my Report project from the solution but I hope Microsoft fixes this issue

这篇关于Visual Studio 2017,JavaScript智能感知不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆