arrayElementDeletions
Reports using the
deleteoperator 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.
Examples
Section titled “Examples”declare const array: number[];delete array[0];const items = [1, 2, 3];delete items[1];declare const matrix: number[][];delete matrix[0][1];declare const array: number[];array.splice(0, 1);const items = [1, 2, 3];items.splice(1, 1);declare const obj: { [key: string]: number };delete obj["key"];declare const map: Map<string, number>;map.delete("key");Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
@typescript-eslint/no-array-delete - Oxlint:
typescript/no-array-delete
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.