Urara-Blog/node_modules/.pnpm-store/v3/files/b8/51114104464c938523fe8772297f65ccb2b7a0cbef8cf7d133a02d4a3abbc6419112436e6ffb6614f3bb6eb0a7744652e99deb04f239f1b33fd378d44d7cf8
2022-08-14 01:14:53 +08:00

66 lines
1.7 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
description: 'Disallow using the `delete` operator on computed key expressions.'
---
> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/no-dynamic-delete** for documentation.
Deleting dynamically computed keys can be dangerous and in some cases not well optimized.
## 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 `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` or `Set` if youre storing collections of objects.
<!--tabs-->
### ❌ Incorrect
```ts
// 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()];
```
### ✅ Correct
```ts
const 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'];
```
## Options
```jsonc
// .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.