0

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)
}
Martin Zeltin
  • 2,496
  • 3
  • 18
  • 36
  • Probably ESLint thinks that your code is JavaScript and not TypeScript. Did you follow [these steps](https://khalilstemmler.com/blogs/typescript/eslint-for-typescript/)? – Christian Vincenzo Traina Sep 14 '22 at 07:45
  • Your ESLint configuration is wrong; you should be using the `@typescript-eslint/` versions of rules like `no-unused-vars`, not the vanilla ones. – jonrsharpe Sep 14 '22 at 07:46
  • Does this answer your question? [ESLint - Configuring "no-unused-vars" for TypeScript](https://stackoverflow.com/questions/57802057/eslint-configuring-no-unused-vars-for-typescript) – jonrsharpe Sep 14 '22 at 07:47

1 Answers1

0

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

Rafik Belkadi
  • 146
  • 1
  • 6