dynamicDeletes
Disallow using the delete operator on computed key expressions.
✅ This rule is included in the ts logical preset.
Deleting dynamically computed keys can be dangerous and is often not well optimized.
Using the delete operator on keys that aren’t runtime constants could be a sign you’re using the wrong data structures.
Consider using a Map or Set if you’re using an object as a key-value collection.
Dynamically adding and removing keys from objects can cause edge case bugs related to hidden properties and non-configurable properties.
Examples
Section titled “Examples”const key = "property";delete obj[key];delete obj[getKey()];const i = 0;delete arr[i];delete obj.property;delete obj["literal"];delete obj[0];const map = new Map<string, number>();map.delete("key");Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”When you know your keys are safe to delete and you’ve considered the performance and safety implications, you may disable this rule for those specific situations.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
@typescript-eslint/no-dynamic-delete - Oxlint:
typescript/no-dynamic-delete
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.