useConsistentMemberAccessibility
¥Summary
-
规则生效日期:
v1.9.0¥Rule available since:
v1.9.0 -
诊断类别:
lint/style/useConsistentMemberAccessibility¥Diagnostic Category:
lint/style/useConsistentMemberAccessibility -
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
¥How to configure
{ "linter": { "rules": { "style": { "useConsistentMemberAccessibility": "error" } } }}¥Description
要求在类属性和方法上使用一致的可访问性修饰符。
¥Require consistent accessibility modifiers on class properties and methods.
TypeScript 允许在类成员前面放置显式 public、protected 和 private 可访问性修饰符。修饰符仅存在于类型系统中,仅用于描述谁被允许访问这些成员。省略可访问性修饰符可以减少读写代码。成员默认是公开的。
¥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
"accessibility": "noPublic"(默认值)
Section titled “"accessibility": "noPublic"(默认值)”¥"accessibility": "noPublic" (default value)
使用以下配置禁止所有显式 public 修饰符:
¥Use the following configuration to disallow all explicit public modifiers:
{ "linter": { "rules": { "style": { "useConsistentMemberAccessibility": { "options": { "accessibility": "noPublic" } } } } }}当 noPublic 时,以下模式被视为错误代码:
¥The following patterns are considered incorrect code with noPublic:
class Animal { public constructor(breed, name) { // ... }}code-block.ts:2:3 lint/style/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/style/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/style/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/style/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/style/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/style/useConsistentMemberAccessibility ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The public modifier is disallowed.
1 │ class Dog {
> 2 │ public walk() {
│ ^^^^^^
3 │ // …
4 │ }
ℹ Remove the accessibility modifier.
"accessibility": "explicit"
Section titled “"accessibility": "explicit"”使用以下配置强制要求尽可能使用显式修饰符:
¥Use the following configuration to enforce the presence of explicit modifiers wherever possible:
{ "linter": { "rules": { "style": { "useConsistentMemberAccessibility": { "options": { "accessibility": "explicit" } } } } }}当 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/style/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.
"accessibility": "none"
Section titled “"accessibility": "none"”使用以下配置禁止所有显式可见性修饰符:
¥Use the following configuration to disallow all explicit visibility modifiers:
{ "linter": { "rules": { "style": { "useConsistentMemberAccessibility": { "options": { "accessibility": "none" } } } } }}当 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/style/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/style/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/style/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/style/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/style/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/style/useConsistentMemberAccessibility ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Accessibility modifiers are disallowed.
1 │ class Dog {
> 2 │ public walk() {
│ ^^^^^^
3 │ // …
4 │ }
ℹ Remove the accessibility modifier.
¥Valid
以下模式被视为具有默认选项 noPublic 的正确代码:
¥The following patterns are considered correct code with the default options noPublic:
{ "linter": { "rules": { "style": { "useConsistentMemberAccessibility": { "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 }}以下模式被视为可访问性设置为 explicit 的正确代码:
¥The following patterns are considered correct code with the accessibility set to explicit:
{ "linter": { "rules": { "style": { "useConsistentMemberAccessibility": { "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 }}以下模式被视为可访问性设置为 none 的正确代码:
¥The following patterns are considered correct code with the accessibility set to none:
{ "linter": { "rules": { "style": { "useConsistentMemberAccessibility": { "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 }}¥Options
该规则支持以下选项:
¥The rule supports the following options:
{ "linter": { "rules": { "style": { "useConsistentMemberAccessibility": { "options": { "accessibility": "explicit" } } } } }}accessibility
Section titled “accessibility”此选项确定类属性和方法上所需的可访问性修饰符。可以将其设置为以下值之一:
¥This option determines the required accessibility modifiers on class properties and methods. It can be set to one of the following values:
-
noPublic- 禁止使用public修饰符。¥
noPublic- forbid the use of thepublicmodifier. -
explicit- 要求每个允许使用辅助功能修饰符的成员都添加辅助功能修饰符。¥
explicit- requires an accessibility modifier for every member where it is permitted. -
none- 禁止所有可访问性修饰符(公共、受保护、私有)。¥
none- forbid all accessibility modifiers (public, protected, private).
默认:noPublic
¥Default: noPublic
¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号