Skip to main content

no-array-delete

Disallow using the delete operator on array values.

💡

此规则报告的一些问题可以通过编辑器 建议 手动修复。

💭

该规则需要 类型信息 才能运行,但这会带来性能方面的权衡。

当将 delete 运算符与数组值一起使用时,数组的 length 属性不受影响,但指定索引处的元素将被删除并在数组中留下一个空槽。这可能会导致意外行为。如 MDN 文档 中所述,从数组中删除元素的推荐方法是使用 Array#splice 方法。

¥When using the delete operator with an array value, the array's length property is not affected, but the element at the specified index is removed and leaves an empty slot in the array. This is likely to lead to unexpected behavior. As mentioned in the MDN documentation, the recommended way to remove an element from an array is by using the Array#splice method.

eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/no-array-delete": "error"
}
});

在线运行试试这个规则 ↗

示例

¥Examples

declare const arr: number[];

delete arr[0];
Open in Playground

选项

该规则不可配置。

何时不使用它

¥When Not To Use It

当你想要允许使用数组表达式的删除运算符时。

¥When you want to allow the delete operator with array expressions.


Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.

See Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.

'## 资源'