Skip to main content

require-no-unchecked-indexed-access

Require "noUncheckedIndexedAccess": true in compilerOptions to include undefined in the type of array index access and index-signature reads.

Targeted pattern scopeโ€‹

The compilerOptions.noUncheckedIndexedAccess field in any tsconfig*.json file.

What this rule reportsโ€‹

This rule reports when noUncheckedIndexedAccess is absent or set to false.

Why this rule existsโ€‹

Without noUncheckedIndexedAccess, accessing an array element by index (e.g., arr[0]) returns the element type T even though the array might be empty. TypeScript assumes the access is in-bounds and produces no warning, so code like:

const first = items[0].name; // No error even if items is empty

compiles without complaint but throws TypeError: Cannot read properties of undefined at runtime.

Enabling noUncheckedIndexedAccess changes the return type of indexed access to T | undefined, forcing developers to check for undefined before using the value โ€” the same pattern required for Map.get() and Record access.

The auto-fixer adds "noUncheckedIndexedAccess": true to compilerOptions.

โŒ Incorrectโ€‹

{
"compilerOptions": {
"strict": true
}
}

noUncheckedIndexedAccess is not part of strict and must be set explicitly.

โœ… Correctโ€‹

{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true
}
}

When not to use itโ€‹

Disable this rule for codebases that rely heavily on index access where the in-bounds guarantee is established by surrounding logic, and the additional undefined checks would add noise without catching real bugs.

Package documentationโ€‹

Rule catalog ID: R021

Further readingโ€‹