Skip to content
← Back to rules

unicorn/no-confusing-array-with 可疑

功能

禁止令人困惑的 Array#with() 用法。

为什么这是不好的?

slice()splice() 等方法不同,Array#with() 会将负索引视为从数组末尾开始计算的偏移量。使用负的静态索引通常是错误的。使用 .length 作为索引总是会产生 undefined,因为有效索引的范围是 0 .. length - 1

示例

此规则认为以下代码是错误的:

javascript
array.with(-1, value);
array.with(array.length, value);

此规则认为以下代码是正确的:

javascript
array.with(array.length - 1, value);
array.with(index, value);

如何使用

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

json
{
  "rules": {
    "unicorn/no-confusing-array-with": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/no-confusing-array-with": "error",
  },
});
bash
oxlint --deny unicorn/no-confusing-array-with

版本

此规则在 v1.73.0 中添加。

参考资料