Skip to content

useConsistentMemberAccessibility

诊断类别:lint/nursery/useConsistentMemberAccessibility

¥Diagnostic Category: lint/nursery/useConsistentMemberAccessibility

自从:v1.9.0

¥Since: v1.9.0

来源:

¥Sources:

要求在类属性和方法上使用一致的可访问性修饰符。

¥Require consistent accessibility modifiers on class properties and methods.

TypeScript 允许在类成员前面放置显式 publicprotectedprivate 可访问性修饰符。修饰符仅存在于类型系统中,仅用于描述谁被允许访问这些成员。省略可访问性修饰符可以减少读写代码。成员默认是公开的。

¥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.

¥Examples

¥Invalid

以下模式被视为具有默认选项 noPublic 的不正确代码:

¥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
}
}

以下模式被视为可访问性设置为 explicit 的不正确代码:

¥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
}
}

以下模式被视为可访问性设置为 none 的不正确代码:

¥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
}
}

¥Valid

以下模式被视为具有默认选项 noPublic 的正确代码:

¥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
}
}

以下模式被视为可访问性设置为 explicit 的正确代码:

¥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
}
}

以下模式被视为可访问性设置为 none 的正确代码:

¥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
}
}

¥Options

该规则支持以下选项:

¥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 - 禁止使用公共(安全修复将删除它)。

    ¥noPublic - forbid the use of public (a safe fix will remove it).

  • explicit - 要求每个允许这样做的成员都有一个可访问性修饰符(安全修复将添加公共修饰符)。

    ¥explicit - requires an accessibility modifier for every member that allows that (a safe fix will add public).

  • none - 禁止所有可访问性修饰符(公共、受保护、私有)。

    ¥none - forbid all accessibility modifiers (public, protected, private).

默认:noPublic

¥Default: noPublic.

¥Related links