dot-notation
Enforce dot notation whenever possible.
在 ESLint 配置 中扩展"plugin:@typescript-eslint/stylistic-type-checked"
可启用此规则。
此规则报告的一些问题可通过 --fix
ESLint 命令行选项自动修复。
该规则需要 类型信息 才能运行,但这会带来性能方面的权衡。
This is an "extension" rule that replaces a core ESLint rule to work with TypeScript. See Rules > Extension Rules.
This rule extends the base dot-notation
rule from ESLint core. 它补充道:
¥It adds:
-
支持可选地忽略计算的
private
和/或protected
成员访问。¥Support for optionally ignoring computed
private
and/orprotected
member access. -
与 TypeScript 的
noPropertyAccessFromIndexSignature
选项兼容。¥Compatibility with TypeScript's
noPropertyAccessFromIndexSignature
option.
如何使用
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
// Note: you must disable the base rule as it can report incorrect errors
"dot-notation": "off",
"@typescript-eslint/dot-notation": "error"
}
});
module.exports = {
"rules": {
// Note: you must disable the base rule as it can report incorrect errors
"dot-notation": "off",
"@typescript-eslint/dot-notation": "error"
}
};
在线运行试试这个规则 ↗
选项
See eslint/dot-notation
's options.
¥Options
该规则添加了以下选项:
¥This rule adds the following options:
interface Options extends BaseDotNotationOptions {
allowPrivateClassPropertyAccess?: boolean;
allowProtectedClassPropertyAccess?: boolean;
allowIndexSignaturePropertyAccess?: boolean;
}
const defaultOptions: Options = {
...baseDotNotationDefaultOptions,
allowPrivateClassPropertyAccess: false,
allowProtectedClassPropertyAccess: false,
allowIndexSignaturePropertyAccess: false,
};
如果 TypeScript 编译器选项 noPropertyAccessFromIndexSignature
设置为 true
,则此规则始终允许使用方括号表示法访问具有 string
索引签名的类型的属性,即使 allowIndexSignaturePropertyAccess
是 false
。
¥If the TypeScript compiler option noPropertyAccessFromIndexSignature
is set to true
, then this rule always allows the use of square bracket notation to access properties of types that have a string
index signature, even if allowIndexSignaturePropertyAccess
is false
.
allowPrivateClassPropertyAccess
Whether to allow accessing class members marked as private
with array notation. Default: false
.
这可能很有用,因为 TypeScript 会在点符号上报告类型错误,但不会在数组符号上报告类型错误。
¥This can be useful because TypeScript will report a type error on dot notation but not array notation.
allowPrivateClassPropertyAccess
设置为 true
时的正确代码示例:
¥Example of a correct code when allowPrivateClassPropertyAccess
is set to true
:
class X {
private priv_prop = 123;
}
const x = new X();
x['priv_prop'] = 123;
Open in PlaygroundallowProtectedClassPropertyAccess
Whether to allow accessing class members marked as protected
with array notation. Default: false
.
这可能很有用,因为 TypeScript 会在点符号上报告类型错误,但不会在数组符号上报告类型错误。
¥This can be useful because TypeScript will report a type error on dot notation but not array notation.
allowProtectedClassPropertyAccess
设置为 true
时的正确代码示例:
¥Example of a correct code when allowProtectedClassPropertyAccess
is set to true
:
class X {
protected protected_prop = 123;
}
const x = new X();
x['protected_prop'] = 123;
Open in PlaygroundallowIndexSignaturePropertyAccess
Whether to allow accessing properties matching an index signature with array notation. Default: false
.
allowIndexSignaturePropertyAccess
设置为 true
时的正确代码示例:
¥Example of correct code when allowIndexSignaturePropertyAccess
is set to true
:
class X {
[key: string]: number;
}
const x = new X();
x['hello'] = 123;
Open in Playground如果 TypeScript 编译器选项 noPropertyAccessFromIndexSignature
设置为 true
,则上述代码始终是允许的,即使 allowIndexSignaturePropertyAccess
是 false
。
¥If the TypeScript compiler option noPropertyAccessFromIndexSignature
is set to true
, then the above code is always allowed, even if allowIndexSignaturePropertyAccess
is false
.
何时不使用它
¥When Not To Use It
如果你出于风格原因特别想要使用两种成员访问类型,或者不希望强制使用一种风格而不是另一种风格,则可以避免此规则。
¥If you specifically want to use both member access kinds for stylistic reasons, or don't wish to enforce one style over the other, you can avoid this rule.
但是,请记住,不一致的风格可能会损害项目的可读性。我们建议为此规则选择一个最适合你的项目的选项。
¥However, keep in mind that inconsistent style can harm readability in a project. We recommend picking a single option for this rule that works best for your project.
Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.
See Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.
'## 资源'
Taken with ❤️ from ESLint core.