noPropertyAccessFromIndexSignature: Explicit Index Access
What you'll learn: How to enforce bracket notation (obj['key']) when accessing properties that exist only in an index signature, preventing silent undefined access bugs.
The Core Idea
When an object type has an index signature, it accepts arbitrary keys. By default, TypeScript lets you access these properties using either dot notation (obj.property) or bracket notation (obj['property']). The problem? Properties defined only by the index signature might not actually exist at runtime, but dot notation makes them look guaranteed.
The noPropertyAccessFromIndexSignature flag forces you to use bracket notation for index-signature-only properties, making the uncertainty explicit.
How It Works
interface DataMap {
knownField: string; // Explicitly defined
[key: string]: string; // Index signature
}
const data: DataMap = {
knownField: "exists"
};
// With noPropertyAccessFromIndexSignature enabled:
data.knownField; // ✅ OK - explicitly defined property
data.unknownField; // ❌ Error - must use bracket notation
data['unknownField']; // ✅ OK - bracket notation allowed
Why This Matters
The analogy: Think of an index signature like a "maybe drawer" in a filing cabinet. Named properties are labeled, permanent drawers. With this flag disabled, TypeScript treats both the same way at access time. Enabled, it forces you to acknowledge uncertainty: "I'm reaching into the maybe drawer."
Without this flag, typos in property names silently become undefined accesses:
interface Config {
[key: string]: boolean;
}
const settings: Config = { darkMode: true };
// Typo - but TypeScript allows it!
if (settings.dakMode) { /* never runs */ }
// With the flag, forced to write:
if (settings['dakMode']) { /* clearer this might not exist */ }
Key Takeaway: Enable noPropertyAccessFromIndexSignature to distinguish between guaranteed properties and uncertain index-signature lookups, forcing explicit bracket notation for the latter and reducing silent typo bugs.