dot-notation
可能な限りドット記法を使用することを強制します。
拡張 "plugin:@typescript-eslint/stylistic-type-checked"
を ESLint設定 で有効にすると、このルールが適用されます。
このルールによって報告される問題の一部は、 ESLintコマンドラインオプション `--fix` によって自動的に修正できます。.
このルールには 型情報 が必要です。
このルールは、基本の eslint/dot-notation
ルールを拡張します。以下の機能を追加します。
- 計算された `private` および/または `protected` メンバーアクセスをオプションで無視するサポート。
- TypeScriptの `noPropertyAccessFromIndexSignature` オプションとの互換性。
使用方法
module.exports = {
"rules": {
// Note: you must disable the base rule as it can report incorrect errors
"dot-notation": "off",
"@typescript-eslint/dot-notation": "error"
}
};
このルールをPlaygroundで試す ↗
オプション
eslint/dot-notation
のオプション を参照してください。
このルールは以下のオプションを追加します。
interface Options extends BaseDotNotationOptions {
allowPrivateClassPropertyAccess?: boolean;
allowProtectedClassPropertyAccess?: boolean;
allowIndexSignaturePropertyAccess?: boolean;
}
const defaultOptions: Options = {
...baseDotNotationDefaultOptions,
allowPrivateClassPropertyAccess: false,
allowProtectedClassPropertyAccess: false,
allowIndexSignaturePropertyAccess: false,
};
TypeScriptコンパイラオプション `noPropertyAccessFromIndexSignature` が `true` に設定されている場合、このルールは `allowIndexSignaturePropertyAccess` が `false` であっても、`string` インデックスシグネチャを持つ型のプロパティに角括弧記法を使用することを常に許可します。
allowPrivateClassPropertyAccess
allowPrivateClassPropertyAccess
が`true`に設定されている場合の正しいコードの例
class X {
private priv_prop = 123;
}
const x = new X();
x['priv_prop'] = 123;
Playgroundで開くallowProtectedClassPropertyAccess
allowProtectedClassPropertyAccess
が`true`に設定されている場合の正しいコードの例
class X {
protected protected_prop = 123;
}
const x = new X();
x['protected_prop'] = 123;
Playgroundで開くallowIndexSignaturePropertyAccess
allowIndexSignaturePropertyAccess
が`true`に設定されている場合の正しいコードの例
class X {
[key: string]: number;
}
const x = new X();
x['hello'] = 123;
Playgroundで開くTypeScriptコンパイラオプション `noPropertyAccessFromIndexSignature` が `true` に設定されている場合、`allowIndexSignaturePropertyAccess` が `false` であっても、上記のコードは常に許可されます。
使用しない場合
スタイル上の理由で、メンバーアクセスの両方の種類を特に使用したい場合、または一方のスタイルを他方よりも優先したくない場合は、このルールを回避できます。
ただし、スタイルの一貫性のなさは、プロジェクトの可読性を損なう可能性があることに注意してください。プロジェクトに最適な単一のオプションを選択することをお勧めします。
型チェック済みlintルールは従来のlintルールよりも強力ですが、型チェック済みlinting の設定も必要です。型チェック済みルールの有効化後にパフォーマンスの低下が発生した場合は、パフォーマンスに関するトラブルシューティング を参照してください。