【问题标题】:Visual Studio Typescript Intellisense doesn't respect tsconfig.json baseUrlVisual Studio Typescript Intellisense 不尊重 tsconfig.json baseUrl
【发布时间】:2018-10-27 00:28:28
【问题描述】:

我在 Visual Studio 2017 中有一个带有大型 TypeScript 代码库的 Web 项目。当我进行 TypeScript 构建时,我没有收到任何错误,并且生成的 JavaScript 是正确的。但是,我的智能感知使用基于 tsconfig.json 中配置的 baseUrl 的路径的任何导入都会显示错误。

例如:

当我将这些更改为相对路径时,错误就会消失:

其他编辑器的代码检查似乎没有前者的问题。我们已经确认它可以在 VS Code 和 JetBrains Rider 中使用。

我的 tsconfig.json 如下:

{
	"compilerOptions": {
		"noImplicitAny": false,
		"noEmitOnError": true,
		"strictNullChecks": true,
		"removeComments": false,
		"sourceMap": true,
		"target": "es5",
		"module": "amd",
		"moduleResolution": "node",
		"baseUrl": "./Content/ts"
	},
	"exclude": [
		"node_modules"
	],
	"compileOnSave": true
}

我尝试了尽可能多的不同 Visual Studio 设置,但似乎没有任何影响 intellisense 是否能够找到模块。同样,编译器本身在查找和构建它们时没有问题。

更新:忘记包含我们还在 .csproj 文件中定义的 <TypeScriptBaseUrl> 属性,设置为与 tsconfig.json 中存在的值相同的值。这会导致 Visual Studio 中的 TS 构建成功,但似乎不会影响 Intellisense。

【问题讨论】:

  • shared/ 下可以添加package.json{"name": "shared"} 吗?
  • @ExplosionPills 添加了那个文件,并包含在VS web项目中,但似乎没有任何效果。
  • 你能创建一个最小的项目,看看问题是否仍然存在,如果它可能是你可能需要报告的错误?
  • @TarunLalwani 刚刚确认我仍然在全新的 MVC 应用程序中看到了这个问题。可能确实需要作为错误提交。

标签: visual-studio typescript


【解决方案1】:

如果有人在 SDK 风格的项目中遇到类似问题,您可以将其添加到您的 .csproj

<!--
  Looks like we need to include tsconfig.json explicitly into the project
  (as opposed to being "auto-included" by being in the project's folder),
  for the Visual Studio IntelliSense to pick it up.
-->
<ItemGroup>
  <Content Include="tsconfig.json" />
</ItemGroup>

假设您的 TypeScript 安装在 node_modules 中,Visual Studio IntelliSense 应该能够将其用作语言服务器,同时尊重 tsconfig.json

另外,我发现禁用 Resharper 对 TypeScript 的支持很有帮助。看起来 Resharper 在支持最新的 TypeScript 版本方面滞后。转到 Extensions / Resharper / Options / Products & Features,取消选中 JavaScript 和 TypeScript。

在完成所有这些更改后重新启动 Visual Studio。

【讨论】:

    【解决方案2】:

    结果表明项目中需要包含 tsconfig.json 文件才能让 Intellisense 满意。不知道为什么 &lt;TypeScriptBaseUrl&gt; 设置不够好。这可能仍然是一个需要报告的错误。

    【讨论】:

      猜你喜欢
      • 2018-07-20
      • 2015-12-04
      • 2020-04-11
      • 2021-09-23
      • 2016-12-06
      • 2017-01-06
      • 1970-01-01
      • 2014-07-17
      • 2016-02-05
      相关资源
      最近更新 更多