Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 233
Francesco Borzi Avatar asked Aug 21 '19 15:08

Francesco Borzi


4 Answers

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;
}
like image 91
Francesco Borzi Avatar answered Oct 13 '22 10:10

Francesco Borzi


Just add the // @dynamic comment like this:

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

More info in the angular docs

like image 21
meblum Avatar answered Oct 13 '22 09:10

meblum


This same error happened to me in an Angular library.

I ignored it by setting "strictMetadataEmit": false, in tsconfig.lib.json under angularCompilerOptions.

like image 44
adrisons Avatar answered Oct 13 '22 10:10

adrisons


I had the same issue trying to return a promise as a function, and replaced this:

export function myFunc(): Function {
    const result = () => ...
    return result;
}

With:

export function myFunc(){
    const result = () => ...
    return result;
}
like image 39
Jnr Avatar answered Oct 13 '22 09:10

Jnr