propertyAccessNotation
Reports bracket notation property access when dot notation can be used.
✅ This rule is included in the tsstylisticandstylisticStrictpresets.
Dot notation (obj.property) is generally preferred over bracket notation (obj["property"]) for accessing object properties.
Dot notation is more concise and easier to read.
Bracket notation should be reserved for cases where it is necessary, such as when the property name contains special characters, is a reserved word, or is computed dynamically.
Examples
Section titled “Examples”const value = obj["property"];
obj["value"] = 123;
const name = person["firstName"];
const nested = data["items"]["first"];class Container { privateProperty = 123;}
const container = new Container();container["privateProperty"] = 123;const value = obj.property;
obj.value = 123;
const name = person.firstName;
const nested = data.items.first;class Container { private privateProperty = 123;}
const container = new Container();
// TypeScript allows private properties to be accessed with quotescontainer["privateProperty"] = 123;// Bracket notation is allowed for non-identifier keysconst value = obj["key with spaces"];
const dashed = obj["key-with-dashes"];
// Bracket notation is allowed for reserved wordsconst reserved = obj["class"];
const keyword = obj["import"];
// Bracket notation is allowed for computed keysconst dynamic = obj[variable];
const computed = obj[getKey()];Options
Section titled “Options”allowIndexSignaturePropertyAccess
Section titled “allowIndexSignaturePropertyAccess”Whether to allow accessing properties matching an index signature with bracket notation.
Type: boolean
Default: false
// eslint ts/propertyAccessNotation: ["error", { allowIndexSignaturePropertyAccess: true }]
interface Config { [key: string]: string;}
const config: Config = {};// ✅ OK with option enabledconst value = config["anyKey"];When Not To Use It
Section titled “When Not To Use It”Disable this rule if your codebase has a convention of using bracket notation for consistency, or if you frequently access properties with names that are reserved words or contain special characters.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
useLiteralKeys - ESLint:
dot-notation@typescript-eslint/dot-notation
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.