Skip to content

propertyAccessNotation

Reports bracket notation property access when dot notation can be used.

✅ This rule is included in the ts stylistic and stylisticStrict presets.

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.

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;

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 enabled
const value = config["anyKey"];

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.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.