Reputation: 1813
I am trying to use fat arrow function with a nestjs decorator in a controller.
So is it possible to do something like this :
@Controller()
export class AppController {
@Get()
findAll = (): string => 'This is coming from a fat arrow !';
}
With this code typescript is telling me this : Unable to resolve signature of property decorator when called as an expression
so it isn't working.
I prefer to use fat arrow function instead of this "traditional" function :
@Controller()
export class AppController {
@Get()
findAll(): string {
return 'This is not comming from a fat arrow';
}
}
This is why I am asking if something like this a possible.
Upvotes: 2
Views: 1717
Reputation: 70432
I would say the answer is no, for the very reason of how the arrow function
differs from a function
in JavaScript, namely changing the lexical this
and arguments
. This answer goes into a lot of detail about how function () {}
differs from myFunction = () => {}
.
Along with that, using arrow functions would disallow a lot of using class functions, so if you are injecting any instances of a service, you would not be able to use them.
@Injectable()
export class AppController {
constructor (private readonly appService: AppService) {}
@Get('/')
hello(): string {
return this.appService.sayHello();
}
}
Would work and return the string from the appService.sayHello()
function, but this
@Injectable()
export class AppController {
constructor (private readonly appService: AppService) {}
@Get('/')
hello = (): string => {
return this.appService.sayHello();
}
}
would not return the string from the appService.sayHello()
function because it does not know about the appService
.
Upvotes: 6