Skip to content
← Back to rules

oxc/bad-char-at-comparison Correctness

This rule is turned on by default.

它的作用

charAt 方法的返回值被用于与长度大于 1 的字符串进行比较时,此规则会发出警告。

为什么这不好?

charAt 方法返回的是长度为 1 的字符串。如果返回值与长度大于 1 的字符串进行比较,那么比较结果将始终为 false。

示例

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

javascript
a.charAt(4) === "a2";
a.charAt(4) === "/n";

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

javascript
a.charAt(4) === "a";
a.charAt(4) === "\n";

如何使用

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

json
{
  "rules": {
    "oxc/bad-char-at-comparison": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "oxc/bad-char-at-comparison": "error",
  },
});
bash
oxlint --deny oxc/bad-char-at-comparison

版本

此规则于 v0.0.22 中添加。

参考资料