Angular: Metadata collected contains an error that will be reported at runtime: Lambda not supported

Francesco Borzi picture Francesco Borzi · Aug 21, 2019 · Viewed 8.2k times · Source

In my Angular app, I'm trying to use a factory provider in my module:

export function getMyFactory(): () => Window {
  return () => window;
}

@NgModule({
  providers: [
    { provide: WindowRef, useFactory: getMyFactory() },
  ],
})
export class MyModule {}

but this is failing with:

Error encountered in metadata generated for exported symbol 'MyModule':

Metadata collected contains an error that will be reported at runtime: Lambda not supported

Answer

Francesco Borzi picture Francesco Borzi · Aug 21, 2019

I've found an easy solution reported on a thread from GitHub: Arrow lambda not supported in static function posted by haochi

The solution is basically:

assigning the result to a variable, then return the variable


So in my case, I've resolved by replacing:

export function getMyFactory(): () => Window {
  return () => window;
}

with:

export function getMyFactory(): () => Window {
  const res = () => window;
  return res;
}