Skip to content

AoT error with @Inject and global interface type #15640

Closed

Description

@DzmitryShylovich

I'm submitting a ... (check one with "x")

[x] bug report => search github for a similar issue or PR before submitting

Current behavior
Looks like the issue with interfaces from #12631 is still present. Ngc doesn't throw for user defined interfaces, but still throws for global interfaces from typescript such as Window or Navigator

Minimal reproduction of the problem with instructions

import {Component, Inject, NgModule, VERSION} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser';

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>
    </div>
  `,
})
export class App {
  name:string;
  constructor(@Inject('test') win: Window) {
    this.name = `Angular! v${VERSION.full}`
  }
}

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App ],
  bootstrap: [ App ],
  providers: [{provide: 'test', useValue: 'value'}]
})
export class AppModule {}

ts 2.2.1

Error:

Failed on type {"filePath":"D:/Workspace/angular-/src/app/app.module.ts","name":"App","members":[]} with error Error: Error encountered resolving symbol values statically. Could not resolv
e type Window (position 14:36 in the original .ts file), resolving symbol App

cc @chuckjaz

Activity

  1. added
    area: core
    on Apr 10, 2017
  2. mtinner commented on Nov 30, 2017

    @mtinner

    Working solution for AoT at least with v5

    import {Component, Inject, NgModule, VERSION} from '@angular/core'
    import {BrowserModule} from '@angular/platform-browser';
    **export const Window_Injection = new InjectionToken('window.injection');**
    
    @Component({
      selector: 'my-app',
      template: `
        <div>
          <h2>Hello {{name}}</h2>
        </div>
      `,
    **providers: [{ provide: Window_Injection, useValue: window }]**
    })
    export class App {
      name:string;
     constructor(**@Inject(Window_Injection) private window**) {
        this.name = `Angular! v${VERSION.full}`
      }
    }
    
    @NgModule({
      imports: [ BrowserModule ],
      declarations: [ App ],
      bootstrap: [ App ],
      providers: [{provide: 'test', useValue: 'value'}]
    })
    export class AppModule {}
    
  3. added this to the Backlog milestone on Jan 23, 2018
  4. RobinBomkampDv commented on Oct 11, 2018

    @RobinBomkampDv

    Having the same issue here.

    I'm injecting Window and Document with it's interfaces as injection token.

    @mtinner 's solution is working for me.

    But I think it is not the right way to do so.

    Is there a way to use interfaces together with @Inject and aot compile?

  5. waterplea commented on Jan 22, 2020

    @waterplea
    Contributor

    I'm trying to inject a native class AudioNode, it's not an interface, it's a class and it is provided by parent component, but AOT compilation fails with Can't resolve all parameters :(

  6. StNekroman commented on Mar 11, 2020

    @StNekroman

    I have this:

    @Pipe({
        name: "routeParam"
    })
    export class GoodFilter {
        static readonly $inject: string[] = [ "$routeParams" ];
    
        private readonly $routeParams: ng.route.IRouteParamsService;
    
        constructor($routeParams: ng.route.IRouteParamsService) {
            this.$routeParams = $routeParams;
        }
    
        @Implement<PipeTransform>()
        public transform(toParse: string): string {
            return UrlBuilder.build(toParse, this.$routeParams);
        }
    }
    

    Leads to error:

    Metadata collected contains an error that will be reported at runtime: Expression form not supported.

    Pointing on constructor argument $routeParams: ng.route.IRouteParamsService

  7. StNekroman commented on Mar 11, 2020

    @StNekroman

    I found, that this bug happens, when I have argument type with dots.
    So when I give to parameter type like "LocationService" - it works, while ng.ILocationService with AOT produces this error.

    Here is workaround.
    I created file "AngularAOTWorkarounds.ts" with next content:
    export type IRouteService = ng.route.IRouteService;

    Then I just import that name alias where I need:
    import {IRouteService} from "./AngularAOTWorkarounds"

    so code will be:

    private readonly $routeParams: ng.route.IRouteParamsService;
    
        constructor($routeParams: IRouteService) {
            this.$routeParams = $routeParams;
        }
    

    But I'm afraid, that I will need to do that for all interface names with dot in my 100500 files =(

  8. added
    area: compiler
    and removed
    area: core
    on Mar 15, 2020
  9. JoostK commented on Mar 15, 2020

    @JoostK
    Member

    @StNekroman That looks like a separate issue, which I believe is still an issue in Ivy. Could you open a new issue with a reproduction, preferably with a Github repo?

    Regarding the reported issue, I attempted to reproduce in Angular 9.0 and I'm unable to: this works in both ViewEngine and Ivy.

  10. dougrday commented on Apr 3, 2020

    @dougrday

    @JoostK I'm still experiencing this error on ng build library --prod

    ------------------------------------------------------------------------------
    Building entry point 'library'
    ------------------------------------------------------------------------------
    Compiling TypeScript sources through ngc
    ERROR: C:/Projects/shared-mc-theme/projects/library/src/lib/theme.service.ts:8:1: Error encountered in metadata generated for exported symbol 'ThemeService':
     C:/Projects/shared-mc-theme/projects/library/src/lib/theme.service.ts:20:45: Metadata collected contains an error that will be reported at runtime: Could not resolve type HTMLDocument.
      {"__symbolic":"error","message":"Could not resolve type","line":19,"character":44,"context":{"typeName":"HTMLDocument"}}
    ...
    

    The relevant code:

    import { DOCUMENT } from "@angular/common";
    import { Inject, Injectable, Renderer2, RendererFactory2 } from "@angular/core";
    import { Observable, of } from "rxjs";
    
    @Injectable({
        providedIn: "root",
    })
    export class ThemeService {
        private renderer: Renderer2;
    
        constructor(
            rendererFactory: RendererFactory2,
            // Dependency-inject the HTML <document> element
            @Inject(DOCUMENT) private document: HTMLDocument,
        ) {
            this.renderer = rendererFactory.createRenderer(null, null);
        }
    
        ...
    }
    

    The current versions I'm using:

        "dependencies": {
            "@angular/animations": "~9.1.0",
            "@angular/common": "~9.1.0",
            "@angular/compiler": "~9.1.0",
            "@angular/core": "~9.1.0",
            "@angular/forms": "~9.1.0",
            "@angular/platform-browser": "~9.1.0",
            "@angular/platform-browser-dynamic": "~9.1.0",
            "@angular/router": "~9.1.0",
            "rxjs": "~6.5.4",
            "tslib": "^1.10.0",
            "zone.js": "~0.10.2"
        }
    
  11. robbiemu commented on Apr 30, 2020

    @robbiemu

    Im getting the same as well with ivy (9.1.1) and --prod: on constructor (@Inject(WINDOW) private window: Window) {

  12. angular-automatic-lock-bot commented on May 31, 2020

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  13. locked and limited conversation to collaborators on May 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: compilercore: difreq1: lowtype: bug/fix

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions