Skip to main content

no-dynamic-delete

Disallows using the delete operator on computed key expressions.

Deleting dynamically computed keys can be dangerous and in some cases not well optimized.

Attributes

  • Included in configs
    • βœ… Recommended
    • πŸ”’ Strict
  • Fixable
    • πŸ”§ Automated Fixer
    • πŸ›  Suggestion Fixer
  • πŸ’­ Requires type information

Rule Details​

Using the delete operator on keys that aren't runtime constants could be a sign that you're using the wrong data structures. Using Objects with added and removed keys can cause occasional edge case bugs, such as if a key is named "hasOwnProperty". Consider using a Map or Set if you’re storing collections of objects.

// 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()];

Options​

// .eslintrc.json
{
"rules": {
"@typescript-eslint/no-dynamic-delete": "warn"
}
}

This rule is not configurable.

When Not To Use It​

When you know your keys are safe to delete, this rule can be unnecessary. Some environments such as older browsers might not support Map and Set.

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.