Visitar URL original
What exactly does it mean that "Function calls are not supported" in AOT metadata · Issue #45753 · angular/angular · GitHub
The Wayback Machine - https://web.archive.org/web/20250603130051/https://github.com/angular/angular/issues/45753
Skip to content

What exactly does it mean that "Function calls are not supported" in AOT metadata #45753

Closed
@elca-lan

Description

@elca-lan

Description

This is more of a request for clarification, but depending on the outcome, the documentation could need some updates. The thing that I don't understand is the section Function calls are not supported.

First of all, I'm reasonably sure that the title is misleading and/or outdated since function calls do very much seem to be allowed in metadata / decorators, since they are commonly used in Angular code and documentation. For example, the documentation of RouterModule contains the common example of

@NgModule({
  imports: [RouterModule.forChild(ROUTES)]
})
class MyNgModule {}

which according to my understanding constitutes a function call (RouterModule.forChild(...)) and is part of a decorator and therefore metadata.

Then, the first actual statement in that documentation section is "The compiler does not currently support function expressions or lambda functions" which again, gives me reason to pause since that is something completely different than function calls.

But even if that's just a confusion that is specific to me, then there is still the fact that everything that this section describes as generating errors, seems to work just fine with Ivy. I've been using older, pre-Ivy versions of Angular for a long time and am generally familiar with the described issues, but as far as I can tell, none of that applies to modern Angular applications anymore.

Is there a bug in my interpretation of the documentation? What is the context that would make my examples fail?

I've been (mostly) sticking to the "approved" patterns in my code, but since it causes significant overhead and is counter intuitive to newcomers, I would like to know how it actually works (or doesn't work).

What is the affected URL?

https://angular.io/guide/aot-metadata-errors#function-calls-not-supported

Please provide the steps to reproduce the issue

Simply create a new Angular project using the Angular CLI and update the content of app.component.ts to

import { Component, Inject, InjectionToken } from '@angular/core';

export const TitleToken = new InjectionToken("TitleToken");

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [
    {
      provide: TitleToken,
      useFactory: () => {
        return `Injected ArrowFactory Title @ ${new Date()}`
      }
    }
  ]
})
export class AppComponent {
  public constructor(
    @Inject(TitleToken) public readonly title: string
  ) {}
}

Please provide the expected behavior vs the actual behavior you encountered

The sample above builds and runs just fine in both development an production mode and the documentation says that it should not.

Please provide a screenshot if possible

No response

Please provide the exception or error you saw

No response

Is this a browser-specific issue? If so, please specify the device, browser, and version.

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentbug

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions