Skip to content
← Back to rules

unicorn/no-confusing-array-with Suspicious

What it does

Disallows confusing uses of Array#with().

Why is this bad?

Array#with() treats a negative index as an offset from the end of the array, unlike methods such as slice() or splice(). Using a negative static index is usually a mistake. Using .length as the index always produces undefined, since valid indices are 0 .. length - 1.

Examples

Examples of incorrect code for this rule:

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

Examples of correct code for this rule:

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

How to use

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

Version

This rule was added in vnext.

References