My journey with typescript has just begun. I had the impression that it was designed to catch mistakes like this, but perhaps my understanding is lacking. The issue arises when attempting something similar to the following:
interface A {
prop: string
}
let a: A
function f() {
console.log(a.prop) // Runtime error
}
f()
No warning appears indicating that a
might be undefined within the function, or suggesting that a
needs immediate definition as it does not match the type A
, or at least prompting for it to be typed as A | undefined
.
If you declare let a: A | undefined
, an error immediately pops up in the function alerting that a
could potentially be undefined. This aligns with what one would expect, if let a: A
also permits undefined values.
Could there be something I am overlooking? Is there a specific setting in tsconfig that should be enabled? I already have strict mode activated.