InterviewHubPRO
Section Overview
Q84

Question 84 of 283

Part 4: JIT vs AOT Compilation, Metadata & TypeScript (76 - 100)

30%
Angular & Signals (283 Qs)•Part 4: JIT vs AOT Compilation, Metadata & TypeScript (76 - 100)

Can I use arrow functions in AOT?

No, Arrow functions or lambda functions can’t be used to assign values to the decorator properties. For example, the following snippet is invalid:

javascript
@Component({
providers: [{
 provide: MyService, useFactory: () => getService()
}]
})

To fix this, it has to be changed as following exported function:

javascript
function getService(){
return new MyService();
}

@Component({
providers: [{
 provide: MyService, useFactory: getService
}]
})

If you still use arrow function, it generates an error node in place of the function. When the compiler later interprets this node, it reports an error to turn the arrow function into an exported function. Note: From Angular5 onwards, the compiler automatically performs this rewriting while emitting the .js file.