Search code examples
typescriptlodash

linter error static method lodash typescript


I have this error reported by the linter on the call to _.isEqual :

https://github.com/typescript-eslint/typescript-eslint/blob/v4.22.0/packages/eslint-plugin/docs/rules/unbound-method.md

Do you know how I can correct this error?

const liste = computed(() => { return _.uniqWith(_.map(liste.value, 'contrat'), _.isEqual) })

Solution

  • There are some options:

    1. As _.isEqual does not use this inside its body you may safely ignore this rule in this particular case:

      /* eslint-disable-next-line @typescript-eslint/unbound-method */
      const liste = computed(() => { return _.uniqWith(_.map(liste.value, 'contrat'), _.isEqual) })
      
    2. You may use wrapping function to preserve calling context:

      const liste = computed(() => { return _.uniqWith(_.map(liste.value, 'contrat'), (a, b) => _.isEqual(a, b)) })
      
    3. Explicitly bind calling context. Kind of ugly but does the job:

      const liste = computed(() => { return _.uniqWith(_.map(liste.value, 'contrat'), _.isEqual.bind(_)) })