ESLint complaining about unused vars in TypeScript function

Viewed 27

I wrote a helper function to remove an array element but my ESLint is complaining that the variables value, index and obj are unused. I tried to set the callback type to just callback: Function but then TypeScript was complaining that the type does not match what it was expecting.

I also tried to replace these values with underscores, since I have a rule in my ESLint config to ignore those but that does not work either.

Perhaps I am doing this completely wrong?

My function is to be used like this (it mutates the original array, which is how I want it):

removeArrayItem(myArray, (item) => item.name === 'johhny')
  1. First attempt

Type 'Function' provides no match for the signature '(value: any, index: number, obj: any[]): unknown'.ts(2345)

const removeArrayItem = (array: any[], callback: Function) => {
    const index = array.findIndex(callback)

    if (index > -1) array.splice(index, 1)
}
  1. Second attempt

'value' is defined but never used. eslint no-unused-vars, 'index' is defined but never used. eslint no-unused-vars, 'obj' is defined but never used. eslint no-unused-vars,

const removeArrayItem = (array: any[], callback: (value: any, index: number, obj: any[]) => unknown) => {
    const index = array.findIndex(callback)

    if (index > -1) array.splice(index, 1)
}
  1. Third attempt (same result as #2)
const removeArrayItem = (array: any[], callback: (_: any, __: number, __: any[]) => unknown) => {
    const index = array.findIndex(callback)

    if (index > -1) array.splice(index, 1)
}
1 Answers

You can preced your unused vars with an underscore _ to let eslint know

Related