Search code examples
javascripteslintdestructuring

Eslint marks variable as unused when destruct array to delete item


I have the following function in Javascript.

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

The code deletes one item from a list, and that works just fine.

The problem is that eslint is marking item as unused variable.

I know that I could suppress this error at the line level, or suppress no-unused-vars all together. But I want to know if there is a more elegant way around it.

After all, the variable is used, why does eslint reporting that error?


Solution

  • After all, the variable is used

    It's not; you never reference item again, so it's correctly detected as unused.

    You need to completely omit the leading item before the comma:

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

    But this looks odd. Some might consider instead:

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