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.

Use the following configuration to disallow all explicit public modifiers:

{
"options": {
"accessibility": "noPublic"
}
}

The following patterns are considered incorrect code with noPublic:

class Animal {
public constructor(breed, name) {
// ...
}
}
code-block.ts:2:3 lint/nursery/useConsistentMemberAccessibility ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The public modifier is disallowed.

1 │ class Animal {
> 2 │ public constructor(breed, name) {
^^^^^^
3 │ // …
4 │ }

Remove the accessibility modifier.

class Animal {
constructor(
public breed,
name,
) {
// ...
}
}
code-block.ts:3:5 lint/nursery/useConsistentMemberAccessibility ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The public modifier is disallowed.

1 │ class Animal {
2 │ constructor(
> 3 │ public breed,
^^^^^^
4 │ name,
5 │ ) {

Remove the accessibility modifier.

class Animal {
public animalName: string;
}
code-block.ts:2:3 lint/nursery/useConsistentMemberAccessibility ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The public modifier is disallowed.

1 │ class Animal {
> 2 │ public animalName: string;
^^^^^^
3 │ }
4 │

Remove the accessibility modifier.

class Pet {
public get name(): string {
return this.animalName;
}
}
code-block.ts:2:3 lint/nursery/useConsistentMemberAccessibility ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The public modifier is disallowed.

1 │ class Pet {
> 2 │ public get name(): string {
^^^^^^
3 │ return this.animalName;
4 │ }

Remove the accessibility modifier.

class Pet {
public set name(value: string) {
this.animalName = value;
}
}
code-block.ts:2:3 lint/nursery/useConsistentMemberAccessibility ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The public modifier is disallowed.

1 │ class Pet {
> 2 │ public set name(value: string) {
^^^^^^
3 │ this.animalName = value;
4 │ }

Remove the accessibility modifier.

class Dog {
public walk() {
// ...
}
}
code-block.ts:2:3 lint/nursery/useConsistentMemberAccessibility ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The public modifier is disallowed.

1 │ class Dog {
> 2 │ public walk() {
^^^^^^
3 │ // …
4 │ }

Remove the accessibility modifier.

Use the following configuration to enforce the presence of explicit modifiers wherever possible:

{
"options": {
"accessibility": "explicit"
}
}

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

class Animal {
constructor( // Invalid: Missing accessibility modifier
public breed,
name,
) {
this.animalName = name;
}
private animalName: string; // OK: Modifier must be present
public get name(): string { // OK: Modifier must be present
return this.animalName;
}
public set name(value: string) { // OK: Modifier must be present
this.animalName = value;
}
protected walk() { // OK: Modifier must be present
// ...
}
}
code-block.ts:2:3 lint/nursery/useConsistentMemberAccessibility ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Missing accessibility modifier on this member.

1 │ class Animal {
> 2 │ constructor( // Invalid: Missing accessibility modifier
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 3 │ public breed,
> 4 │ name,
> 5 │ ) {
> 6 │ this.animalName = name;
> 7 │ }
^
8 │ private animalName: string; // OK: Modifier must be present
9 │ public get name(): string { // OK: Modifier must be present

Use public to explicitly make a member public.

Use the following configuration to disallow all explicit visibility modifiers:

{
"options": {
"accessibility": "none"
}
}

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

class Animal {
protected constructor(breed, name) {
// ...
}
}
code-block.ts:2:3 lint/nursery/useConsistentMemberAccessibility ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Accessibility modifiers are disallowed.

1 │ class Animal {
> 2 │ protected constructor(breed, name) {
^^^^^^^^^
3 │ // …
4 │ }

Remove the accessibility modifier.

class Animal {
constructor(
protected breed,
name,
) {
// ...
}
}
code-block.ts:3:5 lint/nursery/useConsistentMemberAccessibility ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Accessibility modifiers are disallowed.

1 │ class Animal {
2 │ constructor(
> 3 │ protected breed,
^^^^^^^^^
4 │ name,
5 │ ) {

Remove the accessibility modifier.

class Animal {
private animalName: string;
}
code-block.ts:2:3 lint/nursery/useConsistentMemberAccessibility ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Accessibility modifiers are disallowed.

1 │ class Animal {
> 2 │ private animalName: string;
^^^^^^^
3 │ }
4 │

Remove the accessibility modifier.

class Animal {
protected get name(): string {
return this.animalName;
}
}
code-block.ts:2:3 lint/nursery/useConsistentMemberAccessibility ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Accessibility modifiers are disallowed.

1 │ class Animal {
> 2 │ protected get name(): string {
^^^^^^^^^
3 │ return this.animalName;
4 │ }

Remove the accessibility modifier.

class Pet {
private set name(value: string) {
this.animalName = value;
}
}
code-block.ts:2:3 lint/nursery/useConsistentMemberAccessibility ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Accessibility modifiers are disallowed.

1 │ class Pet {
> 2 │ private set name(value: string) {
^^^^^^^
3 │ this.animalName = value;
4 │ }

Remove the accessibility modifier.

class Dog {
public walk() {
// ...
}
}
code-block.ts:2:3 lint/nursery/useConsistentMemberAccessibility ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Accessibility modifiers are disallowed.

1 │ class Dog {
> 2 │ public walk() {
^^^^^^
3 │ // …
4 │ }

Remove the accessibility modifier.

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

{
"options": {
"accessibility": "noPublic"
}
}
class Animal {
constructor(
private breed,
name,
) {
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:

{
"options": {
"accessibility": "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:

{
"options": {
"accessibility": "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