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.
Examples
Section titled ExamplesInvalid
Section titled Invalid"accessibility": "noPublic"
(default value)
Section titled "accessibility": "noPublic" (default value)Use the following configuration to disallow all explicit public
modifiers:
The following patterns are considered incorrect code with noPublic
:
"accessibility": "explicit"
Section titled "accessibility": "explicit"Use the following configuration to enforce the presence of explicit modifiers wherever possible:
The following patterns are considered incorrect code with accessibility
set to explicit
:
"accessibility": "none"
Section titled "accessibility": "none"Use the following configuration to disallow all explicit visibility modifiers:
The following patterns are considered incorrect code with accessibility
set to none
:
Valid
Section titled ValidThe following patterns are considered correct code with the default options noPublic
:
The following patterns are considered correct code with the accessibility set to explicit
:
The following patterns are considered correct code with the accessibility set to none
:
Options
Section titled OptionsThe rule supports the following options:
accessibility
Section titled accessibilityThis 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