π Prefer Array#toSpliced() over Array#splice().
πΌπ« This rule is enabled in the β
recommended config. This rule is disabled in the βοΈ unopinionated config.
π‘ This rule is manually fixable by editor suggestions.
Prefer using Array#toSpliced() over Array#splice() when the return value of splice() is unused.
Array#splice() modifies the original array and returns the removed elements, while Array#toSpliced() returns a changed copy and leaves the original array untouched.
This rule only reports unused splice() calls when the receiver is a let or var variable initialized with a fresh array the scope owns: an array literal, Array()/new Array(), Array.from(), Array.of(), or an array-returning method (.filter(), .flat(), .flatMap(), .map(), .toReversed(), .toSorted(), .toSpliced(), .with()). TypeScript wrappers like array! and array as string[] are supported.
It also skips arrays that escape the scope (passed as an argument, aliased to another binding, returned, or stored in an array or object literal), since an outside reference could observe the in-place mutation.
This rule ignores splice() patterns that have simpler mutating alternatives covered by no-unnecessary-splice, such as array.splice(0, 1) and array.splice(array.length, 0, item).
When TypeScript type annotations or type information show that the receiver is not an array, or that assigning a plain array back could be type-invalid, this rule does not report it. This includes tuples, type aliases, generic type parameters, and readonly array types. Complex inferred TypeScript types require type information to be skipped reliably.
This rule is suggestion-only because assigning the Array#toSpliced() result changes alias behavior. Code that still references the original array object will observe different behavior after reassignment.
// β
let array = [1, 2, 3];
array.splice(1, 1);
// β
array = array.toSpliced(1, 1);// β
let array = items.filter(isActive);
array.splice(1, 1);
// β
let array = items.filter(isActive);
array = array.toSpliced(1, 1);// β
const removed = array.splice(1, 1);// β
β array comes from elsewhere and may be shared, so it must be mutated in place
let structures = getStructures();
structures.splice(index, 1);// β
β array escapes, so an outside holder may rely on the in-place mutation
let array = [];
processItems(array);
array.splice(1, 1);