Solvedangular Angular v5 ngc compiler: Error encountered in metadata generated for exported symbol 'Subscription'

I'm submitting a...

[ ] Regression (a behavior that used to work and stopped working in a new release)
[X ] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see

Current behavior

Building an Angulary library using 5.0.0-beta.4 ngc compiler, I'm getting the following error:

Error: Error: C:/Users/Roberto/Documents/GitHub/angular-library-starter/node_modules/rxjs/Subscription.d.ts:21:1: Error encountered in met
adata generated for exported symbol 'Subscription':
 C:/Users/Roberto/Documents/GitHub/angular-library-starter/node_modules/rxjs/Subscription.d.ts:22:12: Metadata collected contains an error
 that will be reported at runtime: Only initialized variables and constants can be referenced because the value of this variable is needed
 by the template compiler.
  {"__symbolic":"error","message":"Variable not initialized","line":21,"character":11}
    at C:\Users\Roberto\Documents\GitHub\angular-library-starter\node_modules\@angular\tsc-wrapped\src\collector.js:658:27
    at Array.forEach (native)
    at validateMetadata (C:\Users\Roberto\Documents\GitHub\angular-library-starter\node_modules\@angular\tsc-wrapped\src\collector.js:646:
    at MetadataCollector.getMetadata (C:\Users\Roberto\Documents\GitHub\angular-library-starter\node_modules\@angular\tsc-wrapped\src\coll
    at LowerMetadataCache.getMetadataAndRequests (C:\Users\Roberto\Documents\GitHub\angular-library-starter\node_modules\@angular\compiler
    at LowerMetadataCache.ensureMetadataAndRequests (C:\Users\Roberto\Documents\GitHub\angular-library-starter\node_modules\@angular\compi
    at LowerMetadataCache.getMetadata (C:\Users\Roberto\Documents\GitHub\angular-library-starter\node_modules\@angular\compiler-cli\src\tr
    at CompilerHost.upgradeVersion1Metadata (C:\Users\Roberto\Documents\GitHub\angular-library-starter\node_modules\@angular\compiler-cli\
    at CompilerHost.getMetadataFor (C:\Users\Roberto\Documents\GitHub\angular-library-starter\node_modules\@angular\compiler-cli\src\compi
    at StaticSymbolResolver.getModuleMetadata (C:\Users\Roberto\Documents\GitHub\angular-library-starter\node_modules\@angular\compiler\bu

Minimal reproduction of the problem with instructions

Download the following repo:

Upgrade to Angular 5.0.0-beta.4 and run npm run build.

What is the motivation / use case for changing the behavior?

Up to Angular 5.0.0-beta.3 it worked without problems. The library does not use itself rxjs/Subscription.

The only way to avoid the error seems to be to set strictMetadataEmit to false, but it is not recommended.


Angular version: 5.0.0-beta.4


23 Answers

✔️Accepted Answer

Got this problem.
Added // @dynamic before the class that declares static method:

// @dynamic
export class SomeClass {
    public static get() { return 'someValue'; } 

I believe that static methods within classes are used ie for "MyModule.forRoot()", and angular thinks you are defining a module. As your function would not be compatible with AOT, it compilation fails, and the // @dynamic directive is like "don't worry Angular, this class is not intented to be used for bootstapping some modules."

Other Answers:

After upgrading my application to angular 6.0.0-rc.1 and rxjs 6.0.0-rc.0, it seems that the problem has come back when i use ngc.
If i add // @dynamic on node_modules/rxjs/internal/nSubscription.d.ts, it fixes the problem

I had the Expression form not supported error, and it was an inline typing, and it was solved exporting an interface and using it to type that parameter.
Thanks to this article tho:

@tommueller Try below code

export class JsUtils {
    private static entityMap = {
        '&': '&',
        '<': '&lt;',
        '>': '&gt;',
        '"': '&quot;',
        '\'': '&#39;',
        '/': '&#x2F;'
    public static escapeHtml(source: string): string {
       let regEx = /[&<>"'\/]/g; 
       return String(source).replace(regEx, (s) => this.entityMap[s]);

More Issues: