In my code snippet, I am experiencing an issue with TypeScript when I try to access an object property after checking for its existence. The sample code can be found here (when strictNullChecks is enabled).
1. let boolVar: number | undefined;
2.
3. if (boolVar) {
4. boolVar.toString; // all fine
5. }
6.
7. let objectVar: { [key: string]: number | undefined } = {a: 1};
8.
9. const selector = "a";
10. if (objectVar[selector]) {
11. objectVar[selector].toFixed; // Object is possible undefined? o0
12. }
I am confused as to why the TypeScript compiler is throwing the error "Object is possible undefined
" on line 11, considering I have explicitly checked for the object's existence in line 10. Can anyone help me understand this issue?