如何防止 VS 2022 在 esproj 中显示 d.ts 文件的打字稿错误

Erw*_*oly 27 typescript visual-studio-2022

我在 Visual Studio 2022 下为我的 SPA 创建了一个项目 (.esproj)。它构建得很好,但 Visual Studio 显示了很多错误(仅在来自项目的 node_modules 的 .dt.ts 文件以及本地安装的 Typescript 之一)在应用程序数据中)。

这些错误不会显示在 VS Code 上,但理想情况下我会使用 Visual Studio 2022 来实现此目的。

这是我的 esproj

<Project Sdk="Microsoft.VisualStudio.JavaScript.Sdk/0.5.0-alpha">
    <PropertyGroup Label="Globals">
        <ProjectGuid>6b86a87b-eb34-43fe-9cbb-99a2e3db4e41</ProjectGuid>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
        <StartupCommand>set BROWSER=none&amp;&amp;npm start</StartupCommand>
        <JavaScriptTestRoot>src\</JavaScriptTestRoot>
        <JavaScriptTestFramework>Jest</JavaScriptTestFramework>
    </PropertyGroup>
    <ItemGroup>
        <Script Include="**" Exclude="*.esproj;**\node_modules\**" />
    </ItemGroup>

    <!-- This target is copied from the ASP.NET SPA template in order to ensure node_modules are in place. -->
    <Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
        <!-- Ensure Node.js is installed -->
        <Exec Command="node --version" ContinueOnError="true">
            <Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
        </Exec>
        <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
        <Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
        <Exec WorkingDirectory="$(SpaRoot)" Command="npm install --legacy-peer-deps" />
    </Target>
</Project>
Run Code Online (Sandbox Code Playgroud)

这是我的 tsconfig.json

{
  "compilerOptions": {
    "baseUrl": ".",
    "target": "ESNext",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": [
    "src"
  ],
  "exclude": [
    "node_modules"
  ]
}
Run Code Online (Sandbox Code Playgroud)

知道如何配置 Visual Studio 2022 使其不在 .d.ts 上运行分析器吗?

提前致谢

小智 24

这对我有用,更改 .csproj 以在 PropertyGroup 中包含此设置:

<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
Run Code Online (Sandbox Code Playgroud)

  • https://www.typescriptlang.org/docs/handbook/compiler-options-in-msbuild.html#typescriptcompileblocked &lt;Project Sdk="Microsoft.NET.Sdk.Web"&gt; &lt;PropertyGroup&gt; &lt;TargetFramework&gt;net6.0&lt;/ TargetFramework&gt; ... &lt;!--不要构建 typescript.. 消除构建错误--&gt; &lt;TypeScriptCompileBlocked&gt;true&lt;/TypeScriptCompileBlocked&gt; (2认同)

Erw*_*oly 13

更换有效负载部分

 <ItemGroup>
        <Script Include="**" Exclude="*.esproj;**\node_modules\**" />
    </ItemGroup>
Run Code Online (Sandbox Code Playgroud)

经过

  <ItemGroup>
    <Script Include="**"/>
    <Script Remove="**.d.ts"/>
  </ItemGroup>
Run Code Online (Sandbox Code Playgroud)

似乎解决了我的问题

  • 通过额外的支持信息可以改进您的答案。请[编辑]添加更多详细信息,例如引文或文档,以便其他人可以确认您的答案是正确的。您可以[在帮助中心](/help/how-to-answer)找到有关如何写出好的答案的更多信息。 (4认同)

小智 10

  1. 在 Visual Studio 中,右键单击 node_modules 文件夹并选择“从项目中排除”(如果尚未设置)。
  2. 在 VS 中关闭您的解决方案。
  3. 使用文件资源管理器,将 node_modules 文件夹的属性设置为隐藏(不适用于子文件夹)。
  4. 打开您的解决方案。

更快的解决方案加载,不再出现智能感知错误,即使文件夹被隐藏,NPM 仍然可以工作。

  • 碉堡了。当我单击“显示所有文件”时,我在 VS 2022 中收到超长的“未响应”消息。这个解决方案解决了这个问题。 (3认同)