Skip to content
← Back to rules

typescript/no-dynamic-delete Restriction

作用

禁止在计算出的键表达式上使用 delete 运算符。

为什么这很糟糕?

删除动态计算的键可能很危险,而且在某些情况下优化效果并不好。 在不是运行时常量的键上使用 delete 运算符,可能表明你使用了错误的数据结构。 如果你把对象当作键值集合使用,请考虑使用 Map 或 Set。

示例

此规则的错误代码示例:

ts
const container: { [i: string]: 0 } = {};
delete container["aa" + "b"];

此规则的正确代码示例:

ts
const container: { [i: string]: 0 } = {};
delete container.aab;

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "typescript/no-dynamic-delete": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "typescript/no-dynamic-delete": "error",
  },
});
bash
oxlint --deny typescript/no-dynamic-delete

版本

此规则在 v0.5.2 中添加。

参考资料