Validate dictionary lookups
An object has a string index signature but only some keys are populated.
TypeScript · Const
The noUncheckedIndexedAccess compiler option adds undefined to undeclared indexed properties and array element reads. This makes missing keys and out-of-bounds access visible to the type checker.
Enable this compiler option when index signatures or array lookups may refer to missing entries. TypeScript adds undefined to undeclared indexed properties and array elements, requiring a guard or another explicit handling strategy before those values are used.
"noUncheckedIndexedAccess": trueinterface EnvironmentVars {
NAME: string;
OS: string;
[propName: string]: string;
}
declare const env: EnvironmentVars;
const nodeEnv = env.NODE_ENV;
if (nodeEnv !== undefined) {
console.log(nodeEnv.toUpperCase());
}With the option enabled, an undeclared indexed property can be undefined, so the code narrows it before use.
An object has a string index signature but only some keys are populated.
Code reads arrays by calculated positions or loops over uncertain bounds.
A large codebase wants compiler feedback for missing indexed values.
It changes the type of an undeclared property accessed through an index signature by adding undefined. A declared property such as env.NAME keeps its declared string type, while env.NODE_ENV becomes string | undefined.
Follow-up: What happens with arrays? Array indexing is checked more strictly too, so an element read can include undefined even inside a bounds-checked loop.
The option is not included by strict because it can produce noisy diagnostics in codebases that use broad index signatures. Teams should enable it deliberately and assess whether the additional checks fit their code.
Follow-up: How should code handle a possibly missing value? Narrow it with an existence check, or use a non-null assertion only when the invariant is guaranteed elsewhere.
It helps catch out-of-bounds array access and missing keys represented by an index signature. The compiler models those reads as potentially undefined rather than assuming every lookup succeeds.
Follow-up: Does it change explicitly declared keys? No: declared properties retain their declared types; the added undefined applies to undeclared indexed fields and indexed element reads.
index signaturesstrictnon-null assertion operatornoPropertyAccessFromIndexSignature