当 destruct array 删除项目时,Eslint 将变量标记为未使用

Eslint marks variable as unused when destruct array to delete item

我在Javascript中有以下功能。

remove(item) {
    [item, ...this.list] = this.list
}

该代码从列表中删除一项,效果很好。

问题是 eslint 将 item 标记为未使用的变量。

我知道我可以在行级别抑制这个错误,或者一起抑制 no-unused-vars。但我想知道是否有更优雅的解决方法。

毕竟用了变量,为什么eslint会报那个错?

After all, the variable is used

不是;您再也没有引用 item,因此它被正确检测为未使用。

您需要完全省略逗号前的前导项:

remove(item) {
    [, ...this.list] = this.list
}

但这看起来很奇怪。有些人可能会考虑:

remove(item) {
    this.list = this.list.slice(1);
}