How do I prevent VS 2022 to display typescript errors with d.ts files in esproj

Viewed 7293

I created a project (.esproj) for my SPA under visual studio 2022. It build well but visual studio is showing a lot of errors (only on .dt.ts files from node_modules of the project and also from the one of Typescript locally installed in AppData).

The errors are not showing up on VS Code but ideally I would use visual studio 2022 for this.

Here my 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>

here my 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"
  ]
}

Any idea how I can configure visual studio 2022 to not run analyzer on .d.ts ?

Thanks in advance

4 Answers

Replacing the payload part

 <ItemGroup>
        <Script Include="**" Exclude="*.esproj;**\node_modules\**" />
    </ItemGroup>

by

  <ItemGroup>
    <Script Include="**"/>
    <Script Remove="**.d.ts"/>
  </ItemGroup>

seems to solve my issue

This worked for me change .csproj to include this setting inside PropertyGroup:

<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
  1. In Visual Studio, Right click the node_modules folder and select 'Exclude From Project' if not already set.
  2. Close your solution in VS.
  3. Using File Explorer, set the properties of the node_modules folder to Hidden (do not apply to sub folders).
  4. Open your solution.

Faster solution load, no more intellisense errors, NPM still works even though the folder is hidden.

My colleague suggested this fix and its seems to have removed all the typescript errors from the visual studio error list.

  1. Go to the folder where your project is located and look for a folder called "node_modules".

  2. Right click that folder and select "properties."

  3. Click the "Hidden" checkbox.

  4. Click "OK" and a popup menu will appear that asks you if you want to "Apply changes to this folder, subfolders, and files". Select "OK".

All 778 typescript errors disappeared on Visual Studio 2022.

Related