简体   繁体   English

Visual Studio 2017,JavaScript 智能感知不一致

[英]Visual Studio 2017, JavaScript intellisense inconsistencies

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? TL;DR :您如何在 Visual Studio 2017 中包含一个 tsconfig.json 文件,并且仍然像默认情况下一样让 JavaScript 智能感知工作,为您自己的代码和第 3 方库提供代码提示? 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.与 jQuery 非常相似,我希望通过智能感知在 JavaScript 和 html 文件中无处不在地访问我自己的 JavaScript 命名空间。 How does one achieve this?如何实现这一目标?


EDIT 3 :编辑 3
I reported this issue on developercommunity.visualstudio.com .我在developercommunity.visualstudio.com上报告了这个问题。 Please consider up-voting the issue to attract more attention from Microsoft.请考虑对该问题进行投票以吸引微软的更多关注。


EDIT 2 :编辑 2
Currently the "solution" is Resharper, which provides what one would expect straight out-of-the-box.目前,“解决方案”是 Resharper,它提供了人们期望的开箱即用的东西。 But it's not really ideal at a price of $299/yr/user for something Visual Studio markets it provides itself.但对于 Visual Studio 自己提供的市场产品来说,299 美元/年/用户的价格并不是很理想。


EDIT 1:编辑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.如果我对 Visual Studio 中 JavaScript 智能感知的期望不准确,下面是我期望它如何运行的人为示例。

Say I create three TypeScript files in my ./Scripts folder, we'll call them A.ts , B.ts , and C.ts .假设我在 ./Scripts 文件夹中创建了三个 TypeScript 文件,我们将它们命名为A.tsB.tsC.ts Each will contain its own namespace:每个都将包含自己的命名空间:

A.ts A.ts

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

B.ts B.ts

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

C.ts 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.在这个时间点,人们可能期望能够开始看到他们新创建的命名空间的智能感知,事实上,在 A.ts、B.ts 和 C.ts 中,您开始看到这些命名空间的智能感知。 Cool.凉爽的。

在此处输入图像描述

Unfortunately this doesn't carry over to other places, like /Home/Index.cshtml.不幸的是,这不会延续到其他地方,例如 /Home/Index.cshtml。 Index.cshtml knows nothing about the A, B, or C namespaces. Index.cshtml 对 A、B 或 C 命名空间一无所知。 Additionally, .js files know nothing about these namespaces, hence the need for their respective TypeScript Declaration (.d.ts) files.此外,.js 文件对这些命名空间一无所知,因此需要它们各自的 TypeScript 声明 (.d.ts) 文件。

So we'll go ahead and add our tsconfig.json file to the root of our project and configure it like we do above.因此,我们将继续将 tsconfig.json 文件添加到项目的根目录,并像上面那样配置它。 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.如果我们对我们的一个 .ts 文件执行编辑并保存它,这会触发我们将相应的 .d.ts 文件编译到我们的 ./Scripts/out 文件夹中。 Do we now get intellisense in Index.cshtml and .js files?我们现在是否在 Index.cshtml 和 .js 文件中获得智能感知? ...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).现在您可能认为这些文件需要包含在项目中,以便 Visual Studio 将它们用于智能感知(它们在创建时被排除)。 So you include them in the project.因此,您将它们包含在项目中。 Intellisense?智能感知? No. jQuery intellisense?不,jQuery 智能感知? 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.不知何故,我们在 C3.js(注意:一个 JavaScript 文件)中为我们的“A”命名空间获得了智能感知,但其他的都没有。 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...但是然后您重新启动 Visual Studio 实例并...

在此处输入图像描述

Wait....I just had intellisense for the 'A' namespace.等等……我刚刚对“A”命名空间有了智能感知。 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.但是在搞砸了一段时间后,我尝试在 .js 文件的顶部放置一个 import 语句,突然间智能感知开始为导入的文件/模块启动。 Huzzah!嘘!

在此处输入图像描述

And even in Index.cshtml I begin receiving intellisense for our imported namespaces.甚至在 Index.cshtml 中,我也开始接收我们导入的命名空间的智能感知。

在此处输入图像描述

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.但是,当我使用 import 语句关闭 .js 文件时,突然间我在 Index.cshtml 中失去了它们的智能感知。 Wait...what??等等……什么?? And unlike in JavaScript files, I'm unable to write an import statement to receive intellisense for them:与 JavaScript 文件不同,我无法编写导入语句来接收它们的智能感知:

在此处输入图像描述

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.正是这种不一致的来回使我对 Visual Studio 的 JavaScript 智能感知究竟是如何工作的感到困惑。 I still don't know if it's just broken or if I need a better understanding of it.我仍然不知道它是否只是坏了,或者我是否需要更好地理解它。


ORIGINAL POST原帖

System系统
Windows 10视窗 10
VS2017 15.0.0+26228.10 VS2017 15.0.0+26228.10
New JavaScript language service is enabled启用了新的 JavaScript 语言服务

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.几天来,我一直在努力让 Visual Studio 2017 的 JavaScript 智能感知为我工作,但无济于事。 I don't know if something's genuinely wonky with the IDE or if I just don't understand it properly.我不知道 IDE 是否真的有问题,或者我只是没有正确理解它。

My understanding of how VS2017's JS intellisense works is now by typescript definition files, or .d.ts files.我现在对VS2017 的 JS 智能感知如何工作的理解是通过 typescript 定义文件或 .d.ts 文件。 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.最终,我希望获得智能感知来帮助我们使用我们自己的 TypeScript/JavaScript,但现在我会满足于让它在一个新项目中始终如一地工作。

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

After creating the tsconfig.json file JS intellisense ceases to function.创建 tsconfig.json 文件后,JS 智能感知停止运行。 I type a dollar sign ($) into either a .js file or between < script > tags and I get zero intellisense for jQuery.我在 .js 文件或 < script > 标签之间输入一个美元符号 ($),我得到的 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).注意:如果使用 tsconfig.json 配置文件,此功能默认禁用,但可以设置为启用,如下所述)。

"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." “此功能”是指“默认情况下,Salsa 语言服务将尝试检测正在使用的 JavaScript 库……以提供更丰富的 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."好的,所以当存在 tsconfig.json 文件时,Salsa 语言服务不会自动检测正在使用的库并为它们提供智能感知,除非您按照“如下所述”专门配置它。 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."如果我们稍微向下滚动页面并进入 tsconfig 文件设置,我们会遇到一个enableAutoDiscovery设置,它“启用如上所述的定义文件的自动检测和下载”。 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.关于“如上所述”所指的内容并不是很明确,但我只能假设它是指之前的说明,表明重新启用 Salsa 的自动检测功能以提供丰富的智能感知。 So I include that option in my tsconfig.json file and...no benefit;所以我将该选项包含在我的 tsconfig.json 文件中并且......没有任何好处; still no JS intellisense.仍然没有 JS 智能感知。

As it turns out, typingOptions.enableAutoDiscovery has been renamed to typeAcquisition.enable .事实证明, typingOptions.enableAutoDiscovery已重命名为typeAcquisition.enable Grrrrr.呸呸呸。 Alright, so we make that change in our tsconfig.json aaaaaaand... still no JS intellisense.好的,所以我们在 tsconfig.json 中进行了更改 aaaaaa 并且......仍然没有 JS 智能感知。

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.在这一点上,可以编写自己的 .d.ts 文件,Visual Studio 的智能感知会很好(大部分时间......),但对于像 jQuery 这样的 3rd-party 库,我仍然没有智能感知,这是不好。

So far I've landed on the following tsconfig.json file:到目前为止,我已经找到了以下 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/**/*"
  ]
}

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.通过查看 TypeScript 虚拟项目,我可以看到我的项目的 .ts 文件以及 jQuery 都包含在我的 %LOCALAPPDATA%\Microsoft\TypeScript\node_modules@types 目录中。

TypeScript 虚拟项目

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).我希望有了这一切,我将同时拥有我自己项目的 .ts 文件和 3rd 方库的智能感知(至少是那些特别包含的库;此时只有 jQuery)。 Alas...no.唉……不。 Still no jQuery intellisense.仍然没有 jQuery 智能感知。

Incidentally, if I write an import line for jQuery, all of a sudden I get jQuery intellisense顺便说一句,如果我为 jQuery 编写一个导入行,我会突然得到 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.对于模块导入以及 Visual Studio 的智能感知如何处理这些,我并没有搞砸太多,但我真正想要的是在我们的代码和第 3 方库中拥有智能感知应用程序范围。

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.好像是 2017 年 3 月 14 日的最新更新(build 26228.09)导致了这个问题,你可以去控制面板——程序和功能——查看已安装的更新并删除这个更新,然后重新打开 VS 来检查智能感知。 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我注意到 Microsoft Report Projects for Visual Studio Extension 在项目中使用时可能会导致诸如此类的问题,在我将 SQL Server Report 项目添加到解决方案并重新启动 Visual Studio 后,我的几个已经坏了。所以现在我从解决方案中删除了我的报告项目,但我希望微软能解决这个问题

JS files are for javascript and are not transpiled using the typescript compiler. JS 文件用于 javascript,不使用 typescript 编译器进行转译。

If you are using typescript then keep all your typescript in ts files.如果您使用的是打字稿,则将所有打字稿保存在 ts 文件中。

Also, you shouldn't put typescript inside your cshtml files because typescript is not javascript.此外,您不应该将 typescript 放在您的 cshtml 文件中,因为 typescript 不是 javascript。 You need to use the typescript compiler to transpile your typescript into javascript which you can then use that javascript inside your HTML pages.您需要使用 typescript 编译器将您的 typescript 转换为 javascript,然后您可以在 HTML 页面中使用该 javascript。

I think what you are trying to do is not a way typically how people use typescript.我认为您尝试做的不是人们通常使用打字稿的方式。 Usually you write all your typescript in TS files and then as a build process you transpile all your typescript into javascript.通常你将所有的打字稿写在 TS 文件中,然后作为构建过程,你将所有的打字稿转译成 javascript。 There are many strategies on how to do this.关于如何做到这一点有很多策略。 You can bundle all your javascript into one file or break them up into modules.您可以将所有 javascript 捆绑到一个文件中或将它们分解为模块。 There is a lot of options on how you want your final javascript output to look.关于您希望最终 javascript 输出的外观,有很多选择。

Your CSHTML views would reference your compiled javascript and not your typescript.您的 CSHTML 视图将引用您编译的 javascript 而不是您的打字稿。 The browser doesn't know what to do w/ typescript.浏览器不知道用打字稿做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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