Sub topic 12
`noUncheckedIndexedAccess` and `exactOptionalPropertyTypes`
This chapter assumes you know basic TypeScript types, union types, interfaces, and strictNullChecks. These options make TypeScript more precise about two different kinds of absence: a value missing at an index, and a property missing from an object.
Checking indexed access with `noUncheckedIndexedAccess`
With ordinary settings, TypeScript assumes that an indexed access succeeds when the container's type permits the key:
const colors: string[] = ["red", "blue"];
const color = colors[10];
// TypeScript normally infers string
color.toUpperCase();At runtime, colors[10] is undefined. The type string hides that possibility.
Enable noUncheckedIndexedAccess to add undefined to values read through an index:
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true
}
}Now the result has type string | undefined, so it must be narrowed:
const colors: string[] = ["red", "blue"];
const color = colors[10];
if (color !== undefined) {
console.log(color.toUpperCase());
}The option applies to arrays, tuples in some indexed-access situations, and objects accessed with an index signature.
interface Environment {
[name: string]: string;
}
const environment: Environment = {};
const apiUrl = environment.API_URL;
// string | undefined with noUncheckedIndexedAccess
if (apiUrl !== undefined) {
console.log(apiUrl);
}A for...of loop often communicates the intent better than indexing because it gives you existing elements directly.
Exact meaning for optional properties
An optional property uses ?:
interface Settings {
theme?: string;
}Without exactOptionalPropertyTypes, TypeScript generally treats this as allowing both an absent property and an explicitly undefined property:
const first: Settings = {};
const second: Settings = { theme: undefined };Those objects can behave differently at runtime. The in operator and Object.hasOwn can distinguish them:
console.log("theme" in first); // false
console.log("theme" in second); // trueEnable exactOptionalPropertyTypes when that distinction matters:
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true
}
}With the option enabled, theme?: string means the property may be absent, but if it exists, its value must be a string. This assignment becomes an error:
interface Settings {
theme?: string;
}
const settings: Settings = {
theme: undefined
};If explicit undefined is part of the design, include it in the type:
interface Settings {
theme?: string | undefined;
}
const settings: Settings = {
theme: undefined
};The distinction is useful for patch objects. An absent property can mean “leave the old value unchanged,” while undefined can be an intentional value that needs separate handling.
| Type | Property may be absent | Explicit `undefined` allowed | Column |
|---|---|---|---|
| `theme?: string` with exact option | Yes | No | |
| `theme?: string \ | undefined` | Yes | Yes |
| `theme: string \ | undefined` | No | Yes |
How the options work together
These options protect different boundaries:
Use both when your application depends on accurate configuration objects, patch requests, lookup tables, or external data. Expect more compiler errors. Those errors identify places where runtime absence was previously hidden.
Common migration choices
For noUncheckedIndexedAccess, first inspect array indexing, dictionary lookups, and loops that use numeric positions. Prefer one of these fixes:
- Check the result before using it.
- Use a
for...ofloop when position is not important. - Use a tuple when the length and positions are guaranteed.
- Use a non-null assertion only when an invariant is enforced elsewhere and is easy to verify.
For exactOptionalPropertyTypes, inspect object construction and APIs that use optional properties. Decide whether each property means “may be omitted” or “may contain undefined.” Write that decision in the type instead of relying on the compiler's older, looser interpretation.
Exercise
With both options enabled, what is the type of const value = { a: 1 }["b"]; if the object has a string index signature returning number?
Show answer
It includes undefined, because an indexed lookup may not find the requested key. The exact type is number | undefined.