← blog
TypeScriptNovember 27, 2026 · 5 min read

arr[i] Isn't Always There. noUncheckedIndexedAccess Says So.

TypeScript types arr[i] as T, never T | undefined — even though the array might be empty. The noUncheckedIndexedAccess flag makes bracket indexing tell the truth.

Parsa Jiravand · Frontend engineer · building bestpractic
arr[i] Isn't Always There. noUncheckedIndexedAccess Says So.

Three lines of TypeScript. Same shape, same compiler, same strict: true.

TypeScript
1
2
3
const user = users.find(u => u.id === id); // User | undefined const user2 = userMap.get(id); // User | undefined const user3 = usersById[id]; // User

The first two make you prove the user exists before you touch .name. The third doesn't. It's typed as a plain User — no | undefined, no error, no hint that anything could go wrong. Ship it, and the one time id doesn't match a key, you get Cannot read properties of undefined (reading 'name') in production, from a line TypeScript signed off on.

That's not a fluke in one lib's types. It's the default behavior of [].

Say your team hits this bug, traces it back, and "fixes" the type:

TypeScript
1
2
3
4
5
type UsersById = Record<string, User>; const usersById: UsersById = buildIndex(users); const user3 = usersById[id]; user3.name; // still typed `User`, still no warning

Nothing changes. Record<string, User> tells TypeScript that every string key maps to a User — which is exactly the lie that caused the bug. An index signature isn't a promise about what's actually in the object; it's a promise about what could be, and TypeScript takes it at face value for every key you ask for, including ones you made up at runtime from user input, a URL param, or a cache miss.

So the team does what teams do under a ticket deadline: they start writing usersById[id]!.name. The ! tells the compiler "trust me, it's there." It compiles. It ships. It's the exact same crash, now with the compiler's blessing instead of its silence.

arr[i] and obj[key] are indexed access, and TypeScript treats indexed access as a guarantee by default: give it a key of the right type, get back a value of the value type, full stop. It doesn't matter that i could be arr.length or that key could be a string nobody ever inserted — the type signature only describes the shape of a successful lookup, never the possibility of a missing one.

Compare that to Array.prototype.at(), which TypeScript's own lib types declare as at(index: number): T | undefined — not because of any special flag, just because that method's type was written honestly from the start. Or Map.get(), typed V | undefined for the same reason. Those two already tell you the truth. Bracket indexing is the one place TypeScript picked optimism over accuracy, and it's been that way since index signatures existed — strict: true never touched it.

JSONC
1
2
3
4
5
6
// tsconfig.json { "compilerOptions": { "noUncheckedIndexedAccess": true } }

Flip this on (it shipped in TypeScript 4.1, and it is not bundled into strict, so turning on strict mode alone will never give you this) and both lines above change shape:

TypeScript
1
2
3
4
const user3 = usersById[id]; // User | undefined const n = someArray[i]; // T | undefined const t: [string, number] = [...]; const first = t[0]; // still `string` — fixed tuple positions are exempt

Notice what doesn't change: a tuple with a literal, in-bounds index stays exact, because TypeScript can actually verify that position exists at compile time. The flag only widens the cases where it can't — variable array indices and index-signature lookups — which is precisely where the runtime risk lives.

The honest downside: flip this in an existing codebase and you'll likely get a wave of new "possibly undefined" errors, all at once, in code that worked yesterday. That's not the flag being noisy. That's every place your code was already one bad lookup away from the same crash, surfacing at once instead of one ticket at a time.

Resist the urge to ! your way through the list. Each one wants a real answer, and the right one depends on where you are in the code:

TypeScript
1
2
3
4
5
6
7
8
9
10
11
// You can supply a sane fallback const label = tags[0] ?? 'untagged'; // You're about to use it repeatedly — narrow once const user3 = usersById[id]; if (!user3) return notFound(); user3.name; // User, safely // You actually know more than the type system does // (a loop invariant, a length check two lines up) — rare, and worth a comment const first = items[0]!; // items.length checked above

The ! isn't banned — it's just supposed to be the least common answer, reserved for the handful of spots where you can point at why it's safe. If every possibly-undefined error in your list gets the same !, you haven't fixed anything; you've just moved the crash from "silent" to "asserted."

Runs right in your browser — poke at it and watch the concept react live.

noUncheckedIndexedAccess doesn't change a single byte of your compiled JavaScript. The runtime behavior of arr[i] was never in question — it was always capable of returning undefined. All the flag does is make the type checker stop pretending otherwise, for the one kind of access it had been quietly exempting from its own rules.

If your tsconfig.json doesn't have it yet, that's worth fixing before the next usersById[id] ships. What's the last production error you traced back to a lookup TypeScript swore couldn't fail?

Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.


🚀 Want more like this? Every guide, playground, and quiz lives on bestpractic.org — open it and sign up free so the next one finds you.

Thanks for reading! Let's stay connected:

Keep reading

One post a day, in your inbox

Each one with a runnable playground and a quiz. No pitch, no digest, unsubscribe in one click.

0 comments

Sign in to join the discussion, like comments, and save articles for later.