Mismatch "Cannot find name" x "" Typescript error in VS code

My application compiles (transcribes) just fine, but Visual Studio Code still shows a lot of errors:

enter image description here

In some of these cases (e.g. angular and ionic ), the problem is the global variable / namespace, which is added through our inclusion of Angular / Ionic, is not recognized. Most errors are of the form "Cannot find the name" angular / ionic / ng "(etc.).

To make things even weirder, I noticed that the file that was initially opened when loading the VS code had no errors at all. Red underlined errors are in other files in other tabs / editors.

What's happening? How do I get VS Code to consistently recognize that these global / namespaces do exist?

+7
angularjs angular typescript ionic-framework visual-studio-code
source share
3 answers

After many sad days chasing this issue - I finally discovered the GitHub Issue on the GitHub VS Code, which explains what is happening.

TL; dr

My tsconfig.json file was not configured correctly. To fix this, I deleted the files section. You may also have to delete it in your project or just “fix” it to include all the relevant .ts files.

Longer version

Adding a file [section] restricts our project to these two files, and if you open other files that are not referenced from these two files, they will end up in an isolated virtual project . You need to either skip the file section (then all .ts files under the tsconfig.json file are automatically considered part of the project), or you need to list all the files of your project in this section.

My original tsconfig.json file was:

 { "compilerOptions": { "target": "es5", "sourceMap": true, "removeComments": true, "noImplicitAny": true }, "files": [ "typings/index.d.ts", "src/typings/index.d.ts" ] } 

So, VS Code believes that my project consists of only two files. Other .ts files I .ts were considered an “isolated virtual project” - it’s not difficult to understand why they generate errors.

I changed my tsconfig.json file to the following:

 { "compilerOptions": { "target": "es5", "sourceMap": true, "removeComments": true, "noImplicitAny": true } } 

The problem is solved!

+7
source share

In my case, I didn't have tsconfig.json at all!

Creating it with default values ​​fixed the problem.

+1
source share

For me, I also have exactly this problem if I open vscode pointing to the application subdirectory (e.g. 'src'), and not to the root of the application. Maybe this is because typescript.config is in the root directory, but I'm not sure if the reason is this.

0
source share

All Articles