no-array-delete
Disallow using the
deleteoperator on array values.
Extending"plugin:@typescript-eslint/recommended-type-checked" in anESLint configuration enables this rule.
Some problems reported by this rule are manually fixable by editorsuggestions.
This rule requirestype information to run, which comes with performance tradeoffs.
When using thedelete operator with an array value, the array'slength 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 theMDN documentation,the recommended way to remove an element from an array is by using theArray#splice method.
- Flat Config
- Legacy Config
exportdefault tseslint.config({
rules:{
"@typescript-eslint/no-array-delete":"error"
}
});
module.exports={
"rules":{
"@typescript-eslint/no-array-delete":"error"
}
};
Try this rule in the playground ↗
Examples
- ❌ Incorrect
- ✅ Correct
declareconst arr:number[];
delete arr[0];
Open in Playgrounddeclareconst 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 configuringtype checked linting.
SeeTroubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.