no-dynamic-delete
Disallow using the
delete
operator on computed key expressions.
Extending "plugin:@typescript-eslint/strict"
in an ESLint configuration enables this rule.
Some problems reported by this rule are automatically fixable by the --fix
ESLint command line option.
Deleting dynamically computed keys can be dangerous and in some cases not well optimized.
Using the delete
operator on keys that aren't runtime constants could be a sign that you're using the wrong data structures.
Using Object
s with added and removed keys can cause occasional edge case bugs, such as if a key is named "hasOwnProperty"
.
Consider using a
Map
orSet
if youβre storing collections of objects.
module.exports = {
"rules": {
"@typescript-eslint/no-dynamic-delete": "error"
}
};
Try this rule in the playground β
Examplesβ
- β Incorrect
- β Correct
// Can be replaced with the constant equivalents, such as container.aaa
delete container['aaa'];
delete container['Infinity'];
// Dynamic, difficult-to-reason-about lookups
const name = 'name';
delete container[name];
delete container[name.toUpperCase()];
Open in Playgroundconst container: { [i: string]: number } = {
/* ... */
};
// Constant runtime lookups by string index
delete container.aaa;
// Constants that must be accessed by []
delete container[7];
delete container['-Infinity'];
Open in PlaygroundOptionsβ
This rule is not configurable.
When Not To Use Itβ
When you know your keys are safe to delete, this rule can be unnecessary. You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.
Do not consider this rule as performance advice before profiling your code's bottlenecks. Even repeated minor performance slowdowns likely do not significantly affect your application's general perceived speed.