本文へスキップ

explicit-member-accessibility

クラスのプロパティとメソッドに明示的なアクセス修飾子を要求します。

🔧

このルールによって報告される一部の問題は、 ESLintコマンドラインオプション`--fix`によって自動的に修正できます。.

💡

このルールによって報告される一部の問題は、エディターの 提案によって手動で修正できます。.

TypeScriptでは、クラスメンバの前に明示的な`public`、`protected`、`private`アクセス修飾子を配置できます。修飾子は型システムにのみ存在し、これらのメンバにアクセスできる人を記述する役割を果たします。

アクセス修飾子を省略すると、読み書きするコード量が少なくなります。メンバはデフォルトで`public`です。

ただし、多くのクラスを含むコードベースでは、メンバの適切なプライバシーを強制するために、明示的なアクセス修飾子を追加すると役立つ場合があります。また、一部の開発者は、メンバの公開を明示的に保つ方がコードの可読性が高いと考えています。

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/explicit-member-accessibility": "error"
}
};

Playgroundでこのルールを試してみてください ↗

このルールは、どのプロパティを誰が使用できるかをより明確に、そして可読性の高いコードにすることを目的としています。

オプション

このルールは、以下のオプションを受け入れます。

type AccessibilityLevel =
/** Always require an accessor. */
| 'explicit'
/** Never check whether there is an accessor. */
| 'off'
/** Require an accessor except when public. */
| 'no-public';

type Options = [
{
accessibility?: AccessibilityLevel;
ignoredMethodNames?: string[];
overrides?: {
accessors?: AccessibilityLevel;
constructors?: AccessibilityLevel;
methods?: AccessibilityLevel;
parameterProperties?: AccessibilityLevel;
properties?: AccessibilityLevel;
};
},
];

const defaultOptions: Options = [{ accessibility: 'explicit' }];

混合JS/TSコードベースでの設定

TypeScript以外のコード(つまり、`.js`/`.mjs`/`.cjs`/`.jsx`)をlintするコードベースで作業している場合は、ESLintの`overrides`を使用して、`.ts`/`.mts`/`.cts`/`.tsx`ファイルでのみルールを有効にする必要があります。そうしないと、`.js`/`.mjs`/`.cjs`/`.jsx`ファイル内で修正できないlintエラーが報告されます。

{
"rules": {
// disable the rule for all files
"@typescript-eslint/explicit-member-accessibility": "off",
},
"overrides": [
{
// enable the rule specifically for TypeScript files
"files": ["*.ts", "*.mts", "*.cts", "*.tsx"],
"rules": {
"@typescript-eslint/explicit-member-accessibility": "error",
},
},
],
}

`accessibility`

このルールはデフォルトの状態では設定を必要とせず、すべてのクラスメンバにアクセス修飾子が存在することを強制します。暗黙的なpublicメンバを許可したい場合は、次のオプションがあります。

{
"accessibility": "explicit",
"overrides": {
"accessors": "explicit",
"constructors": "no-public",
"methods": "explicit",
"properties": "off",
"parameterProperties": "explicit",
},
}

上記は、使用できる可能性のある設定の例であり、デフォルトの設定ではありません。

オプションが提供されていない場合、以下のパターンは不正なコードと見なされます。

class Animal {
constructor(name) {
// No accessibility modifier
this.animalName = name;
}
animalName: string; // No accessibility modifier
get name(): string {
// No accessibility modifier
return this.animalName;
}
set name(value: string) {
// No accessibility modifier
this.animalName = value;
}
walk() {
// method
}
}
Playgroundで開く

デフォルトオプション`{ accessibility: 'explicit' }`では、以下のパターンは正しいと見なされます。

class Animal {
public constructor(
public breed,
name,
) {
// Parameter property and constructor
this.animalName = name;
}
private animalName: string; // Property
get name(): string {
// get accessor
return this.animalName;
}
set name(value: string) {
// set accessor
this.animalName = value;
}
public walk() {
// method
}
}
Playgroundで開く

accessibilityが**no-public** `[{ accessibility: 'no-public' }]`に設定されている場合、以下のパターンは不正と見なされます。

class Animal {
public constructor(
public breed,
name,
) {
// Parameter property and constructor
this.animalName = name;
}
public animalName: string; // Property
public get name(): string {
// get accessor
return this.animalName;
}
public set name(value: string) {
// set accessor
this.animalName = value;
}
public walk() {
// method
}
}
Playgroundで開く

accessibilityが**no-public** `[{ accessibility: 'no-public' }]`に設定されている場合、以下のパターンは正しいと見なされます。

class Animal {
constructor(
protected breed,
name,
) {
// Parameter property and constructor
this.name = name;
}
private animalName: string; // Property
get name(): string {
// get accessor
return this.animalName;
}
private set name(value: string) {
// set accessor
this.animalName = value;
}
protected walk() {
// method
}
}
Playgroundで開く

`overrides`

オーバーライドは3つの方法で使用できます。

  • 特定のメンバでのpublicの使用を禁止する。
  • ルートが暗黙的なpublicアクセシビリティを許可している場合に、明示的なメンバアクセシビリティを強制する。
  • 特定のメンバ型でのチェックを無効にする。

特定のメンバでのpublicの使用を禁止する

例:`[ { overrides: { constructors: 'no-public' } } ]`

以下のパターンは、例として示したオーバーライドでは不正と見なされます。

class Animal {
public constructor(protected animalName) {}
public get name() {
return this.animalName;
}
}
Playgroundで開く

以下のパターンは、例として示したオーバーライドでは正しいと見なされます。

class Animal {
constructor(protected animalName) {}
public get name() {
return this.animalName;
}
}
Playgroundで開く

特定のメンバに対する明示的なアクセシビリティを要求する

例:`[ { accessibility: 'no-public', overrides: { properties: 'explicit' } } ]`

以下のパターンは、例として示したオーバーライドでは不正と見なされます。

class Animal {
constructor(protected animalName) {}
get name() {
return this.animalName;
}
protected set name(value: string) {
this.animalName = value;
}
legs: number;
private hasFleas: boolean;
}
Playgroundで開く

以下のパターンは、例として示したオーバーライドでは正しいと見なされます。

class Animal {
constructor(protected animalName) {}
get name() {
return this.animalName;
}
protected set name(value: string) {
this.animalName = value;
}
public legs: number;
private hasFleas: boolean;
}
Playgroundで開く

例:`[ { accessibility: 'off', overrides: { parameterProperties: 'explicit' } } ]`

以下のコードは、例として示したオーバーライドでは不正と見なされます。

class Animal {
constructor(readonly animalName: string) {}
}
Playgroundで開く

以下のコードパターンは、例として示したオーバーライドでは正しいと見なされます。

class Animal {
constructor(public readonly animalName: string) {}
}

class Animal {
constructor(public animalName: string) {}
}

class Animal {
constructor(animalName: string) {}
}
Playgroundで開く

例:`[ { accessibility: 'off', overrides: { parameterProperties: 'no-public' } } ]`

以下のコードは、例として示したオーバーライドでは不正と見なされます。

class Animal {
constructor(public readonly animalName: string) {}
}
Playgroundで開く

以下のコードは、例として示したオーバーライドでは正しいと見なされます。

class Animal {
constructor(public animalName: string) {}
}
Playgroundで開く

特定のメンバ型でのチェックを無効にする

例:`[{ overrides: { accessors : 'off' } } ]`

オーバーライドされたメンバ型ではチェックが行われないため、そのメンバ型ではすべての可視性の組み合わせが許可されます。

以下のパターンは、指定された設定では不正と見なされます。

class Animal {
constructor(protected animalName) {}
public get name() {
return this.animalName;
}
get legs() {
return this.legCount;
}
}
Playgroundで開く

以下のパターンは、例として示したオーバーライドでは正しいと見なされます。

class Animal {
public constructor(protected animalName) {}
public get name() {
return this.animalName;
}
get legs() {
return this.legCount;
}
}
Playgroundで開く

`ignoredMethodNames`

特定のメソッドを無視したい場合は、メソッド名を指定して行うことができます。このオプションはコンテキストを考慮しないため、これらの名前を持つすべてのメソッドを無視し、一部のケースを見逃す可能性があります。これは控えめに使用する必要があります。例:`[ { ignoredMethodNames: ['specificMethod', 'whateverMethod'] } ]`

class Animal {
get specificMethod() {
console.log('No error because you specified this method on option');
}
get whateverMethod() {
console.log('No error because you specified this method on option');
}
public get otherMethod() {
console.log('This method comply with this rule');
}
}
Playgroundで開く

使用しない場合

publicをデフォルトとするのが良いデフォルトだと考える場合は、`no-public`設定の使用を検討してください。暗黙的および明示的なpublicメンバを混在させたい場合は、このルールを無効にできます。

ただし、スタイルの一貫性がないと、プロジェクトの可読性が損なわれる可能性があることに注意してください。プロジェクトに最適なこのルールの単一のオプションを選択することをお勧めします。

参考資料

リソース