TypeScript 3.8 will add support for private named instance fields. What about creating a rule for disallowing the use of the private accessibility modifier in favor of the standard hash names for private members?
Examples of incorrect code for this rule:
class MyClass {
private foo = 1;
constructor(private bar: string) {}
}
Examples of correct code for this rule:
class MyClass {
#foo = 1;
#bar: string;
constructor(bar: string) {
this.#bar = bar;
}
}
// Any target whose hash names are supported
// by the TypeScript version in use can be specified
type Target =
// | 'accessors'
// | 'constructors'
// | 'staticMethods'
// | 'instanceMethods'
// | 'staticFields'
| 'instanceFields';
type Options = Target[];
// Defaults to all the targets
const defaultOptions: Options = ['instanceFields']; // for TypeScript 3.8
Perhaps it should allow the opposite as well – enforcing soft (compile-time-only) privacy?
Probably best to be added as an option on the explicit-member-accessibility rule.
Then the options for explicit-member-accessibility should be changed to something like the following?
type AccessibilityLevel =
| 'explicit' // require an accessor (including public)
| 'no-public' // don't require public
| 'off'; // don't check
type PrivateMemberDeclaration =
| 'private' // force private modifier
| '#' // force hash names
| 'off'; // don't check, default
type AccessibilitySetting = AccessibilityLevel | [AccessibilityLevel, PrivateMemberDeclaration];
type Options = {
accessibility?: AccessibilitySetting;
ignoredMethodNames?: string[];
overrides?: {
accessors?: AccessibilitySetting;
constructors?: AccessibilitySetting;
methods?: AccessibilitySetting | {
static?: AccessibilitySetting;
instance?: AccessibilitySetting;
};
properties?: AccessibilitySetting | {
static?: AccessibilitySetting;
instance?: AccessibilitySetting;
};
parameterProperties?: AccessibilitySetting;
};
};
const defaultOptions: Options = {
accessibility: 'explicit',
};
I'm afraid that the name explicit-member-accessibility is a bit confusing for this use case though.
Perhaps it should be renamed to something like member-accessibility and the name explicit-member-accessibility should remain as an alias for it?
Most helpful comment
Then the options for
explicit-member-accessibilityshould be changed to something like the following?I'm afraid that the name
explicit-member-accessibilityis a bit confusing for this use case though.Perhaps it should be renamed to something like
member-accessibilityand the nameexplicit-member-accessibilityshould remain as an alias for it?