Require explicit accessibility modifiers on class properties and methods (explicit-member-accessibil
Leaving off accessibility modifier and making everything public can make your interface hard to use by others. If you make all internal pieces private or protected, your interface will be easier to use.
Rule Details
This rule aims to make code more readable and explicit about who can use which properties.
Options
Configuring in a mixed JS/TS codebase
If you are working on a codebase within which you lint non-TypeScript code (i.e. .js
/.jsx
), you should ensure that you should use ESLint overrides
to only enable the rule on .ts
/.tsx
files. If you don't, then you will get unfixable lint errors reported within .js
/.jsx
files.
accessibility
accessibility
This rule in its default state requires no configuration and will enforce that every class member has an accessibility modifier. If you would like to allow for some implicit public members then you have the following options:
Note the above is an example of a possible configuration you could use - it is not the default configuration.
The following patterns are considered incorrect code if no options are provided:
The following patterns are considered correct with the default options { accessibility: 'explicit' }
:
The following patterns are considered incorrect with the accessibility set to no-public [{ accessibility: 'no-public' }]
:
The following patterns are considered correct with the accessibility set to no-public [{ accessibility: 'no-public' }]
:
Overrides
There are three ways in which an override can be used.
To disallow the use of public on a given member.
To enforce explicit member accessibility when the root has allowed implicit public accessibility
To disable any checks on given member type
Disallow the use of public on a given member
e.g. [ { overrides: { constructors: 'no-public' } } ]
The following patterns are considered incorrect with the example override
The following patterns are considered correct with the example override
Require explicit accessibility for a given member
e.g. [ { accessibility: 'no-public', overrides: { properties: 'explicit' } } ]
The following patterns are considered incorrect with the example override
The following patterns are considered correct with the example override
e.g. [ { accessibility: 'off', overrides: { parameterProperties: 'explicit' } } ]
The following code is considered incorrect with the example override
The following code patterns are considered correct with the example override
e.g. [ { accessibility: 'off', overrides: { parameterProperties: 'no-public' } } ]
The following code is considered incorrect with the example override
The following code is considered correct with the example override
Disable any checks on given member type
e.g. [{ overrides: { accessors : 'off' } } ]
As no checks on the overridden member type are performed all permutations of visibility are permitted for that member type
The follow pattern is considered incorrect for the given configuration
The following patterns are considered correct with the example override
Except specific methods
If you want to ignore some specific methods, you can do it by specifying method names. Note that this option does not care for the context, and will ignore every method with these names, which could lead to it missing some cases. You should use this sparingly. e.g. [ { ignoredMethodNames: ['specificMethod', 'whateverMethod'] } ]
When Not To Use It
If you think defaulting to public is a good default, then you should consider using the no-public
setting. If you want to mix implicit and explicit public members then disable this rule.
Further Reading
TypeScript Accessibility Modifiers
Compatibility
TSLint: member-access
Last updated