Inconsistent "Cannot find name 'x'" Typescript errors in VS Code
After many sad days of chasing this problem - I finally found a GitHub Issue on the VS Code GitHub that explains what is going on.
tl;dr
My tsconfig.json
file was configured incorrectly. To fix it, I removed the files
section. You may need to remove it in your project, as well, or perhaps just "fix" it to include all relevant .ts
files.
Longer version
Adding a files [section] limits our project to those two files and if you open other files not referenced by those two files then they end up in an isolated virtual project. You either need to leave out the files section (then all .ts files underneath the tsconfig.json file are automatically considered part of the project) or you need to list all files of your project in that 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 thought that my project only consisted of two files. Other .ts
files that I loaded were considered an "isolated virtual project" - not hard to understand why they were generating errors.
I changed my tsconfig.json
file to the following:
{
"compilerOptions": {
"target": "es5",
"sourceMap": true,
"removeComments": true,
"noImplicitAny": true
}
}
Problem solved!
In my case, I had no tsconfig.json
at all!
Creating it with default values fixed the problem.