# unicorn/no-confusing-array-with

> Disallow a negative index and `.length` as the index of `Array#with()`

`array.with(index, value)` returns a copy of the array with one element replaced. A negative index counts from the end, and a reader who doesn't know that can't tell which element changes. Passing the array's `.length` as the index looks like a way to append, but `with()` never makes the array longer. It throws a `RangeError`.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"unicorn/no-confusing-array-with" = "error" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad */}
```js
const updated = scores.with(-1, 100);
```

{/* bad */}
```js
const longer = scores.with(scores.length, 100); // throws a RangeError
```

Not reported:

{/* good */}
```js
const updated = scores.with(scores.length - 1, 100);
```

{/* good */}
```js
const longer = [...scores, 100];
```

The rule only sees a number written directly in the call. It doesn't check an index stored in a variable:

{/* good */}
```js
const last = -1;
const updated = scores.with(last, 100);
```
