I’m trying to figure out why TypeScript allows array access in the first case below but not the second. I have noUncheckedIndexedAccess
enabled, but I’m checking that the accessed objects are not undefined
before accessing them.
This doesn’t error:
const arr: number[][] = [[], [], []];
if (arr[0] != undefined) {
arr[0][0] = 42;
}
But this does:
function twoDimArrayAccess(arr: number[][], x: number, y: number) {
if (arr[x] != undefined) {
arr[x][y] = 1;
^^^^^^
// TS2532: Object is possibly undefined
}
}