@typescript-eslint/explicit-member-accessibility
Require public, private or protected on class members
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:
[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.
[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#
class Account {
balanceMissing accessibility modifier on class property balance. = 0;
constructorMissing accessibility modifier on method definition constructor.(readonly ownerMissing accessibility modifier on parameter property owner.: string) {}
}class Account {
private balance = 0;
#history: number[] = [];
public constructor(public readonly owner: string) {}
}Options#
| Option | Default | What 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":
{ "accessibility": "no-public" }1 problemclass Account {
publicPublic accessibility modifier on class property balance. balance = 0;
}{ "accessibility": "no-public" }No problemsclass 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:
{ "accessibility": "no-public" }No problemsclass Account {
constructor(public owner: string) {}
}{ "accessibility": "no-public" }1 problemclass Account {
constructor(publicPublic accessibility modifier on parameter property owner. readonly owner: string) {}
}With overrides, constructors can go without a modifier while everything else needs one:
{ "overrides": { "constructors": "no-public" } }No problemsclass Account {
private balance = 0;
constructor() {}
}