Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท tsconfig: Type Checking Options

TypeScript noPropertyAccessFromIndexSignature option

Reference for noPropertyAccessFromIndexSignature: error TS4111, why dot access on index-signature keys is rejected, and how bracket access makes dynamic keys visible.

An object type with an index signature, such as { [key: string]: number }, accepts any key. TypeScript normally lets you read those keys with dot syntax (stats.visits), which looks the same as reading a declared property. A typo like stats.vistis compiles without complaint.

noPropertyAccessFromIndexSignature makes the difference visible. When it is on, properties that only exist through an index signature must be read with brackets: stats["visits"]. Declared properties still use the dot. The option is not part of strict.

TypeScript
interface Stats {
  total: number;          // declared: stats.total
  [metric: string]: number; // dynamic: stats["visits"]
}

Try it

TypeScript Declared and dynamic keys side by side

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Environment variables, a common case

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

JSON
{ "compilerOptions": { "noPropertyAccessFromIndexSignature": true } }
Part Description
Value true or false. Default: false. Not included in strict. Added in TypeScript 4.2.
Error TS4111: Property 'x' comes from an index signature, so it must be accessed with ['x'].
Affected Dot reads and writes of names that are not declared properties but match a string index signature, including Record<string, T> and process.env style objects.
Not affected Declared properties, bracket access, and methods of the object's prototype.

Usage notes

  • No runtime change. It is a style and safety check; the emitted code is identical.
  • Pairs well with noUncheckedIndexedAccess. One makes dynamic access look different, the other makes it return T | undefined.
  • Typos still compile with brackets, since any string is a valid key. The benefit is that the reader sees "this is a lookup", not that the compiler catches misspellings.
  • Node's process.env is typed with an index signature, so with the option on you write process.env["HOME"].
  • Intersections: if a property is declared on any member of an intersection, dot access is fine.
  • Prefer real types where the keys are known; use a Record of a union of literal keys or an interface, so dot access is checked.