Skip to content

arrayElementDeletions

Reports using the delete operator on array values.

✅ This rule is included in the ts logical presets.

When using the delete operator with an array value, the array’s length property is not affected, but the element at the specified index is removed and leaves an empty slot in the array. This creates a sparse array, which can lead to unexpected behavior when iterating or checking array properties.

The recommended way to remove an element from an array is by using the Array#splice() method, which properly updates the array’s length and shifts remaining elements.

declare const array: number[];
delete array[0];
const items = [1, 2, 3];
delete items[1];
declare const matrix: number[][];
delete matrix[0][1];

This rule is not configurable.

If you intentionally want to create sparse arrays with empty slots, you can disable this rule. Some codebases may use sparse arrays for specific algorithms or memory optimization purposes.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.