deny-soft-private-modifier
This plugin disallows the use of soft private modifier.
- ⭐️ This rule is included in Flat Config
rdlabo.configs.recommended.- ✒️ The
--fixoption on the command line can automatically fix some of the problems reported by this rule.
TypeScript's private modifier is only enforced at compile time. It can still be accessed at runtime through bracket notation or by casting to any. JavaScript hard-private fields (#) are runtime-enforced and cannot be bypassed from outside the class. This rule replaces private properties and methods with # fields and updates this.x references to this.#x.
Rule Details
This rule checks classes for the following patterns:
- A
privateproperty definition (private field = ...) - A
privatemethod definition (private method() { ... }) - A
this.fieldreference wherefieldwas declared asprivate
It does not report constructors, because private constructor() has a different meaning (preventing external instantiation). A private readonly property is reported; the fix removes private, adds #, and preserves readonly.
The rule auto-fixes by:
- Removing the
privatekeyword. - Inserting
#before the property or method name. - Updating all
this.fieldorthis.method()references in the class tothis.#fieldorthis.#method().
Examples
Incorrect
class TokenStore {
private token = '';
private refresh() {
this.token = 'new-token';
}
}
Correct
class TokenStore {
#token = '';
#refresh() {
this.#token = 'new-token';
}
}
Options
This rule has no options.
When to enable
Enable this rule when a project wants runtime-enforced encapsulation for class internals. It is safe to run with --fix on existing code, but it changes public API surface: any code that was relying on compile-time private access at runtime will break.