Skip to content
← Back to rules

typescript/dot-notation 样式

💭 This rule requires type information.

它的作用

在属性访问能够安全地写成 obj.prop 时,强制使用点号表示法。

为什么这很糟糕?

对于静态属性名,点号表示法通常比方括号表示法更易读、更简洁。

示例

以下是此规则的错误代码示例:

ts
obj["name"];
foo["bar"];

以下是此规则的正确代码示例:

ts
obj.name;
foo.bar;

obj[key];
obj["not-an-identifier"];

配置

此规则接受一个包含以下属性的配置对象:

allowIndexSignaturePropertyAccess

type: boolean

default: false

允许对由索引签名覆盖的属性使用方括号表示法。

allowKeywords

type: boolean

default: true

允许对 ES3 关键字属性名使用方括号表示法(例如 obj["class"])。

allowPattern

type: string

default: ""

允许使用方括号表示法的属性名的正则表达式模式。

allowPrivateClassPropertyAccess

type: boolean

default: false

允许对私有类成员使用方括号表示法。

allowProtectedClassPropertyAccess

type: boolean

default: false

允许对受保护的类成员使用方括号表示法。

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "options": {
    "typeAware": true
  },
  "rules": {
    "typescript/dot-notation": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  options: { typeAware: true },
  rules: {
    "typescript/dot-notation": "error",
  },
});
bash
oxlint --type-aware --deny typescript/dot-notation

版本

此规则于 v1.49.0 中添加。

参考