noArrayDelete (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.9 - Diagnostic Category:
lint/nursery/noArrayDelete - This rule has an unsafe fix.
- The default severity of this rule is error.
- This rule belongs to the following domains:
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noArrayDelete": "error" } } }}Description
Section titled “Description”Disallow using the delete operator on array elements.
The delete operator removes a property from an object.
When you use it on an array element, the element is removed, but the array doesn’t get shorter
and the elements after it don’t move.
The array is left with an empty slot, also called a “hole”, at that position:
const array = [1, 2, 3];delete array[1];console.log(array); // [1, <1 empty item>, 3]console.log(array.length); // 3Array methods treat holes in different ways. For example, forEach() skips them, while
for...of loops and includes() treat them as undefined.
To remove elements from an array, use the
splice()
method instead.
The rule needs type information to know whether a value is an array, so it only reports values that are known to be arrays or tuples.
The fix replaces delete array[index] with array.splice(index, 1). It’s unsafe, because
splice() moves the elements after the removed one to lower positions, and it returns the
removed elements instead of true.
The fix isn’t offered when the expression contains comments, or when the code doesn’t use
semicolons and removing delete would join the statement to the previous line.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”declare const array: number[];delete array[0];/invalid-array.ts:2:1 lint/nursery/noArrayDelete FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Avoid using the delete operator on array elements.
1 │ declare const array: number[];
> 2 │ delete array[0];
│ ^^^^^^^^^^^^^^^
3 │
ℹ Deleting an array element leaves an empty slot in the array and doesn't change its length.
ℹ Use splice() to remove the element instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use splice() instead.
1 1 │ declare const array: number[];
2 │ - delete·array[0];
2 │ + array.splice(0,·1);
3 3 │
declare const tuple: [string, number];delete tuple[1];/invalid-tuple.ts:2:1 lint/nursery/noArrayDelete FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Avoid using the delete operator on array elements.
1 │ declare const tuple: [string, number];
> 2 │ delete tuple[1];
│ ^^^^^^^^^^^^^^^
3 │
ℹ Deleting an array element leaves an empty slot in the array and doesn't change its length.
ℹ Use splice() to remove the element instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use splice() instead.
1 1 │ declare const tuple: [string, number];
2 │ - delete·tuple[1];
2 │ + tuple.splice(1,·1);
3 3 │
declare const array: number[];array.splice(0, 1);
declare const object: Record<string, number>;delete object["key"];See Also
Section titled “See Also”- To report
deleteon the properties of any object, without type information, seenoDelete. That rule avoidsdeletebecause it can make code slower. It doesn’t report keys that are variables, such asdelete array[index], and its fix assignsundefined, which leaves the array length unchanged. Both rules reportdelete array[0].
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.