I'm having trouble getting Typescript to detect an error in the code snippet below:
function foo() {
console.log(this.x.y)
}
foo()
When I run it using npx ts-node a.ts
, there are no Typescript errors displayed, but it naturally fails with
TypeError: Cannot read properties of undefined (reading 'y')
Is there a way to make Typescript flag the absence of this.x
in this code as a compilation error rather than a runtime error?
This is how my ts-config.json
looks like (it might not be perfect):
{
"compilerOptions": {
"esModuleInterop": true,
"moduleResolution": "node",
"target": "es2022",
},
"include": ["/**/*.ts"],
"exclude": ["node_modules"],
"files": ["a.ts"]
}
I'm running Node version 18.13.0, typescript version 5.2.2, and ts-node version 10.9.1 (all installed via npm).