Get rid of eslint@typescript-eslint/no-unused-vars error when index is not used in map function typescript

900 Views Asked by At

I got this code:

const reducer = (element:number, index: number) => [element]; //es lint error.
const positionsArray = $.map(this.positions, reducer);

I am casting a Float32Array (it is this.positions) to a js array. I get the error: 'index' is defined but never used.eslint@typescript-eslint/no-unused-vars

I cannot apply the "// eslint-disable-line no-unused-vars" (which btw I dont think is a pleasant solution)

How to get rid of this error for this case that I don't need the index? I checked reduce and try to retrieve the accumulator but did not achieve it. Thanks.

EDIT: If I dont define the index I get this error: enter image description here

1

There are 1 best solutions below

4
On BEST ANSWER

Don't define the parameter. Neither TypeScript nor JavaScript requires you to use all parameters that a callback might accept.

You can also convert the typed array to a normal array by invoking the iterator - it looks like jQuery's .map doesn't behave well with it, so use plain JS instead:

const positionsArray = [...this.positions].map(element => [element]);

If you just want to get a plain array from the typed array, don't use .map, just invoke the iterator alone:

const positionsArray = [...this.positions];