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

5

28

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

Permanency answered 15/12, 2021 at 9:47 Comment(2)
Same, 322 errors when I build, all from node_modules. What makes it slightly worse is I'm definitely not compiling any typescript in VS, webpack handling all that.Izanagi
I want to share this which is my solution for this issue.Wive
Z
26

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

<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
Zajac answered 12/4, 2022 at 16:25 Comment(2)
This is definitely the best fix for newer .net core and .net 5+ projects.Begot
typescriptlang.org/docs/handbook/… <Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net6.0</TargetFramework> ... <!--do not build typescript.. get rid of build errors--> <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>Lithosphere
P
13

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

Permanency answered 23/12, 2021 at 22:15 Comment(3)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Phuongphycology
I tried this approach and the typescript errors still appear.Winstead
this worked for me! However, I noticed that it was duplicating some of my files (i.e. package.json) - so I removed the <Script Include="**"/> and then everything displayed correctly and there were no TS errors. If I had to guess I had the include somewhere else in my CSProj file.Divebomb
G
10
  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.

Gabbard answered 8/3, 2022 at 21:38 Comment(1)
Holy crap. I was having super long "Not Responding" messages in VS 2022 when I clicked "Show All Files". This solution fixed that.Rabat
W
4

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.

Winstead answered 20/8, 2022 at 0:16 Comment(2)
Nice, a bit strange but it just works. Just make the node_modules folder hidden (top folder is enough, no need to let it change all files inside the folder). Extra benefit is that is stays local, so it does not add any changes to the repository.Album
This is great. My node_modules were not listed under a specific project in Visual Studio 2022 - only included because I am using Karma / Jasmine to do Javascript TDD. Given that the folder was only visible in the filesystem this was just the easy answer I needed. Thanks!Clary
I
0

Go to the current user directory

start/run %USERPROFILE% or C:\Users\<user>\

Find the folder called node_modules

Delete it

rebuild the project

Indigotin answered 24/4, 2024 at 11:46 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.