Skip to main content

require-no-property-access-from-index-signature

Require "noPropertyAccessFromIndexSignature": true in compilerOptions to enforce bracket notation for properties defined by index signatures, distinguishing them from explicitly declared properties.

Targeted pattern scopeโ€‹

The compilerOptions.noPropertyAccessFromIndexSignature field in any tsconfig*.json file.

What this rule reportsโ€‹

This rule reports when compilerOptions.noPropertyAccessFromIndexSignature is absent or explicitly set to false.

Why this rule existsโ€‹

TypeScript allows two ways to access a property on an object:

  • Dot notation: obj.key โ€” suggests key is a known, declared property
  • Bracket notation: obj["key"] โ€” suggests the key may not be declared

When an object type has an index signature, TypeScript normally allows both notations interchangeably:

interface Config {
strict: boolean; // declared property
[key: string]: unknown; // index signature
}

declare const cfg: Config;
cfg.strict; // declared โ€” fine
cfg.unknownKey; // index signature โ€” TypeScript allows this without the flag

With noPropertyAccessFromIndexSignature: true, TypeScript requires bracket notation for any access that is satisfied by an index signature rather than a declared property:

cfg.strict; // โœ… declared property โ€” dot notation allowed
cfg["unknownKey"]; // โœ… index-signature access โ€” bracket notation required
cfg.unknownKey; // โŒ error โ€” must use bracket notation for index-sig access

This distinction has two practical benefits:

  1. Visual clarity: Dot notation signals "I know this property exists". Bracket notation signals "this may or may not exist at runtime".
  2. Interaction with noUncheckedIndexedAccess: When both flags are enabled, bracket-notation index accesses produce T | undefined rather than T, making the potential absence of the value explicit.

The auto-fixer adds "noPropertyAccessFromIndexSignature": true to compilerOptions.

โŒ Incorrectโ€‹

{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true
}
}

noPropertyAccessFromIndexSignature is absent; dot notation is silently allowed for index-signature accesses.

{
"compilerOptions": {
"noPropertyAccessFromIndexSignature": false
}
}

Explicitly disabled.

โœ… Correctโ€‹

{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true,
"noPropertyAccessFromIndexSignature": true
}
}

Index-signature accesses must use bracket notation.

When not to use itโ€‹

Disable this rule in projects that access index-signature properties with dot notation throughout the codebase and where migrating to bracket notation is not immediately feasible. The rule pairs best with noUncheckedIndexedAccess: true for maximum benefit.

Package documentationโ€‹

Rule catalog ID: R035

Further readingโ€‹