简体   繁体   English

Visual Studio 2017:js 文件导致许多打字稿错误

[英]Visual Studio 2017: js file causes many typescript errors

Please see the edit below请参阅下面的编辑

I have used the following package as a base for my VS Project -> https://github.com/AngularClass/angular2-webpack-starter我使用以下包作为我的 VS 项目的基础 -> https://github.com/AngularClass/angular2-webpack-starter

Everything worked nicely in Visual Studio code, I then tried to convert it to a Visual Studio 2017 Project (node.js web)一切在 Visual Studio 代码中运行良好,然后我尝试将其转换为 Visual Studio 2017 项目(node.js web)

Now some problems appeared现在出现了一些问题

VS seems to install TypeScript definitions into a special folder C:\Users\MyUser\AppData\Local\Microsoft\TypeScript\node_modules\@types VS 似乎将 TypeScript 定义安装到一个特殊的文件夹C:\Users\MyUser\AppData\Local\Microsoft\TypeScript\node_modules\@types

Additionally I included @types/node in my package.json which resulted in the appropriate node_modules folder.此外,我在 package.json 中包含了 @types/node ,这导致了相应的 node_modules 文件夹。

now VS complains with error code TS4090 (Conflicting defintions for 'node').现在 VS 抱怨错误代码 TS4090(“节点”的定义冲突)。

Is there a possibility apart from deleting the folder in \AppData to tell VS which @types it should use?除了删除 \AppData 中的文件夹来告诉 VS 它应该使用哪些 @types 之外,是否还有可能?

The whole integration of TypeScript isn't really clear to me... Does MSBuild recognize an existing tsconfig.json? TypeScript 的整个集成对我来说并不是很清楚...... MSBuild 能识别现有的 tsconfig.json 吗? or would I have to configure it in in the project-file as seen here ?还是我必须在项目文件中配置它,如此处所示

EDIT编辑

I just noticed that I was actually editing a .js file.我刚刚注意到我实际上正在编辑一个 .js 文件。 As soon as I changed the extension to .ts every warning and error vanished.一旦我将扩展名更改为 .ts,每个警告和错误都会消失。 I also got many typescript errors (like TS2307, can't find module '@angular/core') before changing the extension from .js to .ts在将扩展名从 .js 更改为 .ts 之前,我也遇到了许多打字稿错误(如 TS2307,找不到模块'@angular/core')

The file just did an import (from node)该文件刚刚进行了导入(从节点)

import * as fs from 'fs';

Could anyone explain why I got this errors?谁能解释为什么我得到这个错误? Why a JS files causes 200+ typescript errors (some don't even seem related to the import from above)?为什么一个 JS 文件会导致 200 多个打字稿错误(有些甚至与上面的导入无关)?

EDIT2 the errors which occur are EDIT2 发生的错误是

  • TS2403 - Subsequent Variable declarations must have the same type. TS2403 - 后续变量声明必须具有相同的类型。
  • TS2300 - Duplicate identifier 'PropertyKey' TS2300 - 重复标识符“PropertyKey”
  • TS4090 - Conflicting definitions for 'node' found (paraphrasing: found in ./node_modules and C:\Users\MyUser\AppData\Local\Microsoft\TypeScript\node_modules\@types TS4090 - 发现“节点”的定义冲突(释义:在./node_modulesC:\Users\MyUser\AppData\Local\Microsoft\TypeScript\node_modules\@types

All the errors are caused by d.ts files in C:\Users\MyUser\AppData\Local\Microsoft\TypeScript\node_modules\@types or C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TypeScript\lib.es6.d.ts所有错误都是由 C:\Users\ d.ts C:\Users\MyUser\AppData\Local\Microsoft\TypeScript\node_modules\@typesC:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TypeScript\lib.es6.d.ts中的 d.ts 文件引起的C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TypeScript\lib.es6.d.ts

like Bowden Kelly noted, this seems the cause for this problems seems to be the definition files MS put in here C:\Users\MyUser\AppData\Local\Microsoft\TypeScript\node_modules\@types , as soon as I install my own definition files in ./node_modules VS finds both definitions (if present) when editing JS files.就像鲍登凯利指出的那样,这似乎是造成这个问题的原因似乎是 MS 放在这里的定义文件C:\Users\MyUser\AppData\Local\Microsoft\TypeScript\node_modules\@types ,只要我安装我自己的定义./node_modules中的文件 VS 在编辑 JS 文件时找到两个定义(如果存在)。 So the solution would either be:所以解决方案要么是:

  • doing it the MS Way以 MS 方式进行
  • somehow exclude MS Directory以某种方式排除 MS 目录

This is a BUG related in https://github.com/Microsoft/TypeScript/issues/14565这是https://github.com/Microsoft/TypeScript/issues/14565中相关的 BUG

I was able to eliminate the errors by enabling我能够通过启用消除错误

在此处输入图像描述

For JavaScript, try to uncheck/disable the "new JavaScript language service" under Tools > Options > Text Editor > JavaScript/TypeScript > Language Service .对于 JavaScript,请尝试取消选中/禁用Tools > Options > Text Editor > JavaScript/TypeScript > Language Service下的“新 JavaScript 语言服务”。 This seemed to help me when I opened random JavaScript files and the error window would go crazy.当我打开随机的 JavaScript 文件并且错误窗口会变得疯狂时,这似乎对我有所帮助。

Regarding the Angular 2 errors (cannot find module @angular/core), if there is a tsconfig.json in the project directory both Visual Studio and the TS compiler will use that.关于 Angular 2 错误(找不到模块 @angular/core),如果项目目录中有 tsconfig.json,Visual Studio 和 TS 编译器都会使用它。 If you close an reopen the solution everything on the TypeScript tab of project properties should be disabled.如果您关闭并重新打开解决方案,则项目属性的 TypeScript 选项卡上的所有内容都应该被禁用。

Once you know tsconfig.json is working, if you are using TS 2.x the compiler should find and use the types that come with angular 2 under node_modules/@angular/core/index.d.ts , unless you have a setting in tsconfig that overrides this like types: [] .一旦您知道 tsconfig.json 正在工作,如果您使用的是 TS 2.x,编译器应该在node_modules/@angular/core/index.d.ts下找到并使用 angular 2 附带的类型,除非您在tsconfig 覆盖了类似的types: []

I have still not been able to figure out 100% what is going on in Visual Studio Errors window since I still get sporadic unjustified errors (I know this because I can still compile).我仍然无法 100% 弄清楚 Visual Studio 错误窗口中发生了什么,因为我仍然会收到零星的不合理错误(我知道这一点是因为我仍然可以编译)。 I do know that the Visual Studio TypeScript Language Service is what controls errors and intellisense in the Visual Studio IDE and it is tied to a specific version of TypeScript (2.1 in VS 2017).我确实知道 Visual Studio TypeScript 语言服务是控制 Visual Studio IDE 中的错误和智能感知的东西,它与特定版本的 TypeScript(VS 2017 中的 2.1)相关联。 The errors have always caused problems in both VS 2015 and now VS 2017 also.这些错误总是在 VS 2015 和现在的 VS 2017 中引起问题。 I think it comes down to the inner workings of the extremely undocumented TypeScript Virtual Projects, for which I have still not found a purpose.我认为这归结为极其无证的 TypeScript 虚拟项目的内部运作,我仍然没有找到目的。

These links are useful reading:这些链接是有用的阅读:

http://blog.angular-university.io/typescript-2-type-system-how-do-type-definitions-work-in-npm-when-to-use-types-and-why-what-are-compiler-opt-in-types/ http://blog.angular-university.io/typescript-2-type-system-how-do-type-definitions-work-in-npm-when-to-use-types-and-why-what-are-编译器选择类型/

http://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types http://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types

https://github.com/Microsoft/TypeScript/issues/14540 https://github.com/Microsoft/TypeScript/issues/14540

The definition files installed to C:\Users\MyUser\AppData\Local\Microsoft\TypeScript\node_modules\@types are used for giving JavaScript IntelliSense only.安装到C:\Users\MyUser\AppData\Local\Microsoft\TypeScript\node_modules\@types的定义文件仅用于提供 JavaScript IntelliSense。 If you have TypeScript in your project or a project configured with a tsconfig.json , you need to fetch your own .d.ts files and store them in your project.如果您的项目中有 TypeScript 或配置了tsconfig.json的项目,您需要获取自己的.d.ts文件并将它们存储在您的项目中。 Luckily this project comes with all the .d.ts files you need already in the package.json a simple npm install should do the trick.幸运的是,这个项目在package.json中包含了你需要的所有 .d.ts 文件,一个简单的npm install就可以解决问题。

I can tell you that the conflicting definitions with 'node' are likely due to having 2 copies of the node.d.ts file.我可以告诉你,与“节点”冲突的定义很可能是由于 node.d.ts 文件有 2 个副本。 If you manually installed one yourself, on top of the one included in the projects package.json , then you'll get those errors.如果您自己手动安装了一个,在项目package.json中包含的那个之上,那么您将收到这些错误。

I'm not certain about the TS errors you are getting in your JavaScript file.我不确定您在 JavaScript 文件中遇到的 TS 错误。 Can you show me an example of one of the errors and a snippet of the code causing it?你能告诉我一个错误的例子和导致它的代码片段吗?

Also you probably don't want integration with MSBuild as this project is already setup to run with npm scripts + webpack.此外,您可能不希望与 MSBuild 集成,因为该项目已经设置为使用 npm 脚本 + webpack 运行。 You should check out this plugin: https://marketplace.visualstudio.com/items?itemName=MadsKristensen.NPMTaskRunner你应该看看这个插件: https ://marketplace.visualstudio.com/items?itemName=MadsKristensen.NPMTaskRunner

Finally I don't think you want to be inside a Node.js web project.最后,我认为您不想进入 Node.js Web 项目。 For this project, you are probably best off just opening the folder in VS.对于这个项目,您最好只在 VS 中打开文件夹。 To do that just use File > Open > Folder... and navigate to your root folder.为此,只需使用File > Open > Folder...并导航到您的根文件夹。 This will give you all the editing tools you need without any MSBuild or VS project structure.这将为您提供所需的所有编辑工具,而无需任何 MSBuild 或 VS 项目结构。 Considering this project wasn't designed to take advantage of any of those features, it'll probably be easier just to ignore them for now.考虑到该项目并非旨在利用这些功能中的任何一个,因此暂时忽略它们可能会更容易。

Please let me know what other issues you run into.请让我知道您遇到的其他问题。

仅供参考,这已在 Visual Studio 17.2 中修复🙂

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

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