no-array-delete
Disallow using the
delete
operator on array values.
Extending "plugin:@typescript-eslint/recommended-type-checked"
in an ESLint configuration enables this rule.
Some problems reported by this rule are manually fixable by editor suggestions.
This rule requires type information to run.
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 is likely to lead to unexpected behavior. As mentioned in the
MDN documentation,
the recommended way to remove an element from an array is by using the
Array#splice
method.
module.exports = {
"rules": {
"@typescript-eslint/no-array-delete": "error"
}
};
Try this rule in the playground ↗
Examples
- ❌ Incorrect
- ✅ Correct
declare const arr: number[];
delete arr[0];
Open in Playgrounddeclare const arr: number[];
arr.splice(0, 1);
Open in PlaygroundOptions
This rule is not configurable.
When Not To Use It
When you want to allow the delete operator with array expressions.
Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting. See Troubleshooting > Linting with Type Information > Performance if you experience performance degredations after enabling type checked rules.