@typescript-eslint/explicit-member-accessibility

Require public, private or protected on class members

fixable

Also called typescript-eslint/explicit-member-accessibility, typescript/explicit-member-accessibility

A class member without a modifier is public. A reader can't tell whether that was a decision or an oversight, and a helper that was meant to be private becomes part of the class's API. This rule makes you write the modifier on every property, method, accessor, constructor and parameter property.

Members with a #private name are never reported.

The rule doesn't fix a missing modifier, because only you know which one is right. The report offers all three as suggestions. With "no-public", --fix removes the public keyword.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"@typescript-eslint/explicit-member-accessibility" = "error" # or "warn"

Every option, set to its default. The ones that are commented out are unset by default.

bunfig.toml
[lint.rules]
"@typescript-eslint/explicit-member-accessibility" = ["error", {
  accessibility = "explicit", # "explicit", "no-public" or "off"
  overrides = {
    # accessors = <"explicit", "no-public" or "off">,
    # constructors = <"explicit", "no-public" or "off">,
    # methods = <"explicit", "no-public" or "off">,
    # parameterProperties = <"explicit", "no-public" or "off">,
    # properties = <"explicit", "no-public" or "off">,
  },
  ignoredMethodNames = [], # list of strings
}]

Examples#

example.ts3 problems
class Account {
  balance = 0;

  constructor(readonly owner: string) {}
}
example.tsNo problems
class Account {
  private balance = 0;
  #history: number[] = [];

  public constructor(public readonly owner: string) {}
}

Options#

OptionDefaultWhat it does
accessibility"explicit""explicit" requires a modifier. "no-public" reports public, since it's the default anyway. "off" checks nothing.
overrides{}A different level for one kind of member: accessors, constructors, methods, properties, parameterProperties.
ignoredMethodNames[]Names of methods and accessors that are never reported.

With "no-public":

example.tswith { "accessibility": "no-public" }1 problem
class Account {
  public balance = 0;
}
example.tswith { "accessibility": "no-public" }No problems
class Account {
  balance = 0;
  private pin = "";
}

A parameter needs a modifier to become a property. So "no-public" allows public there when it's the only modifier, and reports it next to readonly:

example.tswith { "accessibility": "no-public" }No problems
class Account {
  constructor(public owner: string) {}
}
example.tswith { "accessibility": "no-public" }1 problem
class Account {
  constructor(public readonly owner: string) {}
}

With overrides, constructors can go without a modifier while everything else needs one:

example.tswith { "overrides": { "constructors": "no-public" } }No problems
class Account {
  private balance = 0;

  constructor() {}
}