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 const value: any
value = const obj: any
obj["property"];
const obj: any
obj["value"] = 123;
const const name: any
name = const person: any
person["firstName"];
const const nested: any
nested = const data: any
data["items"]["first"];class class Container
Container { Container.privateProperty: number
privateProperty = 123;}
const const container: Container
container = new constructor Container(): Container
Container();const container: Container
container["privateProperty"] = 123;const const value: any
value = const obj: any
obj.any
property;
const obj: any
obj.any
value = 123;
const const name: any
name = const person: any
person.any
firstName;
const const nested: any
nested = const data: any
data.any
items.any
first;class class Container
Container { private Container.privateProperty: number
privateProperty = 123;}
const const container: Container
container = new constructor Container(): Container
Container();
// TypeScript allows private properties to be accessed with quotesconst container: Container
container["privateProperty"] = 123;// Bracket notation is allowed for non-identifier keysconst const value: any
value = const obj: any
obj["key with spaces"];
const const dashed: any
dashed = const obj: any
obj["key-with-dashes"];
// Bracket notation is allowed for reserved wordsconst const reserved: any
reserved = const obj: any
obj["class"];
const const keyword: any
keyword = const obj: any
obj["import"];
// Bracket notation is allowed for computed keysconst const dynamic: any
dynamic = const obj: any
obj[const variable: any
variable];
const const computed: any
computed = const obj: any
obj[const getKey: any
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 interface Config
Config { [key: string
key: string]: string;}
const const config: Config
config: interface Config
Config = {};// ✅ OK with option enabledconst const value: string
value = const config: Config
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 ❤️🔥 around the world by
the Flint team and contributors.