Skip to content

useConsistentMemberAccessibility

Diagnostic Category: lint/nursery/useConsistentMemberAccessibility

Since: v1.9.0

Sources:

Require consistent accessibility modifiers on class properties and methods.

TypeScript allows placing explicit public, protected, and private accessibility modifiers in front of class members. The modifiers exist solely in the type system and just serve to describe who is allowed to access those members. Leaving off accessibility modifiers makes for less code to read and write. Members are public by default.

However, adding in consistent accessibility modifiers can be helpful in codebases with many classes for enforcing proper privacy of members. Some developers also find it preferable for code readability to keep member publicity explicit.

The following patterns are considered incorrect code with the default options noPublic:

class Animal {
public constructor(
public breed,
name,
) {
// Parameter property and constructor
this.animalName = name;
}
public animalName: string; // Property
public get name(): string {
// get accessor
return this.animalName;
}
public set name(value: string) {
// set accessor
this.animalName = value;
}
public walk() {
// method
}
}

The following patterns are considered incorrect code with the accessibility set to explicit:

class Animal {
// Constructor is not set accessibility modifier
constructor(
public breed,
name,
) {
// Parameter property and constructor
this.animalName = name;
}
private animalName: string; // Property
public get name(): string {
// get accessor
return this.animalName;
}
public set name(value: string) {
// set accessor
this.animalName = value;
}
protected walk() {
// method
}
}

The following patterns are considered incorrect code with the accessibility set to none:

class Animal {
constructor(
protected breed,
name,
) {
// Parameter property and constructor
this.name = name;
}
// Property is set accessibility modifier
private animalName: string; // Property
get name(): string {
// get accessor
return this.animalName;
}
// set accessor is set accessibility modifier
set name(value: string) {
// set accessor
this.animalName = value;
}
// walk() is set accessibility modifier
protected walk() {
// method
}
}

The following patterns are considered correct code with the default options noPublic:

class Animal {
constructor(
public breed,
name,
) {
// Parameter property and constructor
this.animalName = name;
}
private animalName: string; // Property
get name(): string {
// get accessor
return this.animalName;
}
set name(value: string) {
// set accessor
this.animalName = value;
}
protected walk() {
// method
}
}

The following patterns are considered correct code with the accessibility set to explicit:

class Animal {
public constructor(
public breed,
name,
) {
// Parameter property and constructor
this.animalName = name;
}
private animalName: string; // Property
public get name(): string {
// get accessor
return this.animalName;
}
public set name(value: string) {
// set accessor
this.animalName = value;
}
protected walk() {
// method
}
}

The following patterns are considered correct code with the accessibility set to none:

class Animal {
constructor(
breed,
name,
) {
// Parameter property and constructor
this.name = name;
}
animalName: string; // Property
get name(): string {
// get accessor
return this.animalName;
}
set name(value: string) {
// set accessor
this.animalName = value;
}
walk() {
// method
}
}

The rule supports the following options:

{
"//": "...",
"options": {
"accessibility": "explicit"
}
}

This option determines the required accessibility modifiers on class properties and methods. It can be set to one of the following values:

  • noPublic - forbid the use of public (a safe fix will remove it).
  • explicit - requires an accessibility modifier for every member that allows that (a safe fix will add public).
  • none - forbid all accessibility modifiers (public, protected, private).

Default: noPublic.