In VSCode, IntelliSense is saying a property does not exist on an object in a TypeScript file even when I specify the optional chaining syntax to short-circuit the error. The code compiles correctly to JavaScript es2020.
Here is the typescript code
"use strict"
const animals = {
dog: {
name: 'Rover'
}
};
const catName: string = animals.cat?.name;
console.log(catName);
const catName = animals.cat?.name; ** *<<<<< the squiggly line appears under cat***
console.log(catName);
The actual TypeScript error is
error TS2339: Property 'cat' does not exist on type '{ dog: { name: string; }; }'.
I understand that the VSCode ships with its own version of Typescript so I added the following to my workspace settings.
{
"typescript.tsdk": "node_modules/typescript/lib",
}
When I hover over the typescript version number on the VSCode status bar it shows the following which is a valid path.
D:projectstest-tsnode_modulestypescriptlibtsserver.js
I also tried disabling all VSCode Extensions however this did not change the result.
My VSCode version is 1.88.1
My TypeScript version is 5.45
FYI, I’m using Windows 10.
All my research and review of other posts lead back to the typescript’s “typescript.tsdk” setting but this has not solved the problem.
Below is my tsconfig.json file.
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"rootDir": "./src",
"outDir": "./js",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"include": [
"src"
]
}
How can I eliminate the error that is being displayed in VSCode?