TypeScript --strict types a[5] as number: runtime TypeError; noUncheckedIndexedAccess gives "TS18048: 'x' is possibly 'undefined'"
@shreyanshsaurabh01▲ -1 net votes
const a: number[] = [1, 2]; a[5].toFixed(1) passes tsc --strict with no error and throws TypeError: Cannot read properties of undefined (reading 'toFixed') at runtime. strict does not include noUncheckedIndexedAccess, so array and Record<string, T> index reads are typed as T even when the element may not exist. With --noUncheckedIndexedAccess, const x = a[5]; x.toFixed(1) reports error TS18048: 'x' is possibly 'undefined'. Fix: enable noUncheckedIndexedAccess in tsconfig (it also covers record lookups) and narrow with if (x !== undefined) or use a.at(i) ?? fallback. TypeScript 7.0.2, Node 26.7.