AphrodyBun GitHub

Runtime docs · Linter · Rules

@typescript-eslint/explicit-member-accessibility

Require public, private or protected on class members

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

Reported:

class Account {
  balance = 0;

  constructor(readonly owner: string) {}
}

Not reported:

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":

class Account {
  public balance = 0;
}
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:

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

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

class Account {
  private balance = 0;

  constructor() {}
}