No Unchecked Indexed Access

Index reads become T | undefined under noUncheckedIndexedAccess: guard array and Record lookups explicitly and narrow without assertions.

TypescriptData Integrity

Code

typescriptsnippet
24 lines
// Enable "noUncheckedIndexedAccess": true in tsconfig, then every index
// access becomes T | undefined until you prove the key exists
const readings: number[] = [18, 42, 7];

const first = readings[0]; // number | undefined under the flag
console.log("first via guard:", first === undefined ? "empty" : first + 1);

// Object index access gets the same protection
const ranks: Record<string, number> = { gold: 1, silver: 2 };
const rank = ranks["bronze"]; // number | undefined
console.log("bronze rank:", rank ?? "unranked");

// Array.prototype.at already returns T | undefined, so it reads the same
const last = readings.at(-1);
console.log("last:", last ?? "none");

// After a real check the type narrows - no non-null assertion needed
if (rank !== undefined) {
  console.log("narrowed rank:", rank + 10);
}

// map results are never undefined per element, but indexed reads still are
const doubled = readings.map((value) => value * 2);
console.log("doubled:", doubled[1] ?? "missing");

Output

Captured from a sandboxed run of this snippet in the build pipeline.

first via guard: 19
bronze rank: unranked
last: 7
doubled: 84

Reading the code

The file’s state is a handful of top-level bindings: first, rank, last, doubled.

The top-level object literal pins gold, silver as plain values — no derivation, no imports behind them.

What to watch for

  • No dependencies section needed; the standard library is the whole story.

Putting it to use

Look for the first via guard:, bronze rank:, last: prefixes in the run’s output; the prefixes map lines to steps.

Core lines

const doubled = readings.map((value) => value * 2);

Interactive Tools for This Topic

FAQ

Do I need to install anything before running this?
No — every API used here ships with the language runtime, so there is nothing to install. Its state lives in module-level bindings: `first`, `rank`, `last`.