0

Following is some code and we are facing lint issues:-

export function TakeUntilDestroy(destroyMethodName = 'ngOnDestroy') {
    return function <T extends { new(...args: any[]): {} }>(constructor: T) {
      // some code
      }
}

I am getting two lint issues on 2nd line of the code. First one is "non-arrow functions are forbidden". We don't want to remove the lint rule to get rid of this error.

2nd lint issue is "Type literal has only a call signature - use 'new(...args: any[]) => {}' instead.

I am not able to resolve the issues. I tried few things but could not resolve the same. These issues are new to me.

ShareYourExp
  • 11
  • 1
  • 1
  • 5

1 Answers1

1

You're defining a new function with the syntax return function, the first rule asserts you should use arrow function instead.

return <T extends { new(...args:any[]): {} }>(constructor: T) => {

See documentation on arrow functions here

The second error has to do with your generic type T. It looks like that is also expecting an arrow function, i.e.

return <T extends { new(...args:any[]) => {} }>(constructor: T) => {

do errors show up if you replace the code you posted with this snippet?

export function TakeUntilDestroy(destroyMethodName = 'ngOnDestroy') {
    return <T extends { new(...args: any[]) => {} }>(constructor: T) => {
      // some code
    }
}
dracstaxi
  • 2,417
  • 1
  • 13
  • 12
  • I know what is the issue. Unfortunately documentation is not helpful as I cant find any such scenario. This scenario is quiet complex. – ShareYourExp Aug 12 '20 at 16:56
  • can you share more context? The code and linting errors seem straightforward. I was also missing the arrow function in my first answer, I edited it to correct the mistake – dracstaxi Aug 12 '20 at 17:09
  • It has to do with how functions are declared. In your example you're using the `function(...args){...code}` declaration syntax. You need to use the arrow syntax instead `(...args) => {...code}` – dracstaxi Aug 12 '20 at 19:22
  • Sorry my bad. I was entangled in work so much that I overlook the solution you provide. It has sorted the first issue but the 2nd issues remains. How I could miss such a simple thing, remove keyword "function" and add a "=>". – ShareYourExp Aug 13 '20 at 12:54
  • 2nd issue is converted into ";" expected. It needs to have a semicolon. – ShareYourExp Aug 13 '20 at 13:02
  • 1
    Again I did not understood the issue correctly. thanks a lot for the help. – ShareYourExp Aug 13 '20 at 15:41