computed-property-spacing
Configuration
Enforce consistent spacing inside computed property brackets.
Rule Details
This rule covers every place a computed name expression appears inside square brackets:
- Member access
obj[foo]and optional member accessobj?.[foo] - Object literal keys
{ [foo]: 1 } - Object destructuring patterns
const { [foo]: x } = obj - Class member names (methods, accessors, fields,
accessorproperties) - TypeScript indexed access types
type T = A[B]
Multi-line forms are exempt — the rule only fires when the opening [ and its first inner token (or the closing ] and its last inner token) sit on the same source line.
Options
This rule has a string option:
"never"(default) disallows spaces inside computed property brackets."always"requires one space inside computed property brackets.
This rule has an object option:
"enforceForClassMembers": true(default) — also enforce the spacing rule on class member names. Set tofalseto leave class members unchecked.
never
Examples of incorrect code for this rule with the default "never" option:
Examples of correct code for this rule with the default "never" option:
always
Examples of incorrect code for this rule with the "always" option:
Examples of correct code for this rule with the "always" option:
enforceForClassMembers
When set to false, class member names are exempt regardless of the "never" / "always" mode.
Examples of correct code for this rule with "never", { "enforceForClassMembers": false }: