ProgramGuru

noUncheckedIndexedAccess

noUncheckedIndexedAccess — why it matters

With noUncheckedIndexedAccess enabled, unchecked array indexing and access through index signatures include undefined when the key might not exist. This encourages guards before using values. It does not check array bounds at runtime or rewrite the indexing operation.

What you’ll learn

  • Explain nouncheckedindexedaccess and the problem it solves.
  • Identify the relevant compile-time checks and runtime responsibilities.
  • Run the example, predict its output, and try a realistic modification.

Understand the mechanism

The array has only indexes 0 and 1. The guard ensures both an existing numeric entry and an out-of-bounds undefined value are handled safely, with or without the extra compiler flag.

Array access with an index typed number is not proof that the index exists.

Follow the concept flow

noUncheckedIndexedAccess — concept flowscores[index] → number | undefined → Handle out-of-rangenoUncheckedIndexedAccess1scores[index]2number | undefined3Handle out-of-rangeCompiler configuration changes checks and output, not runtime guarantees.
Follow the responsibilities from input to checked result.

Run the primary TypeScript example

Save as nouncheckedindexedaccess.ts. The following program passed strict TypeScript compilation, and the displayed output was captured by executing its emitted JavaScript with Node.js 22.16.0.

TypeScript source

const scores = [9, 7];
function readScore(index: number): string {
  const score = scores[index];
  return score === undefined ? "Missing" : `Score: ${score}`;
}
console.log(readScore(1));
console.log(readScore(4));

Verified output

Score: 7
Missing

Compile: npx tsc --strict --target es2022 --module commonjs nouncheckedindexedaccess.ts; run: node nouncheckedindexedaccess.js. For Node.js API examples, install @types/node first. For browser projects, use the browser-specific instructions below.

The example demonstrates the code-level behavior; the related configuration or test commands are shown separately when needed.

Trace the result

The array has only indexes 0 and 1. The guard ensures both an existing numeric entry and an out-of-bounds undefined value are handled safely, with or without the extra compiler flag.

Predict what changes if you alter one input. Then run the code to compare your reasoning with the real output.

Common mistake and correction

Array access with an index typed number is not proof that the index exists.

Identify whether the failure is a type-checking issue, a configuration issue, or a runtime behavior issue before choosing a fix.

Practice the concept

Compile once with --noUncheckedIndexedAccess and once without. Remove the guard and compare diagnostics.

Write down your expected result first. For configuration examples, compare compiler output and diagnostics; for test examples, make the assertion fail deliberately to verify that the tool reports it.

Check your understanding

What should you remember when configuring TypeScript compilation?
Choose one answer. You can try again.

Answer and explanation

Compiler options control static checks or emitted files; running the result is a separate step.

The array has only indexes 0 and 1. The guard ensures both an existing numeric entry and an out-of-bounds undefined value are handled safely, with or without the extra compiler flag.

Official reference

For the exact current options and platform behavior, consult the official documentation. These examples were verified using the tool versions described above.

What to remember

With noUncheckedIndexedAccess enabled, unchecked array indexing and access through index signatures include undefined when the key might not exist.

Run the primary example, then complete the practice task or environment-specific lab.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: exactOptionalPropertyTypes →