Skip to content

AoT compiler does not recognize type Window (possibly issue with global variables and/or TypeScript built-in libs) #14050

Closed

Description

@Meligy

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

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

The following would fail:

export function getWindow() { return window; }

@NgModule({
  declarations: [
    AppComponent,
    SimpleRouteComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    AppRoutingModule
  ],
  providers: [
    {provide: 'window', useFactory: getWindow}
  ],
  bootstrap: [AppComponent]
})
export class AppModule {
  constructor(@Inject('window') w: Window) {
    console.log(w);
  }

}

With error

ERROR in Error encountered resolving symbol values statically. Could not resolve type Window (position 29:36 in the original .ts file), resolving symbol AppModule in /Users/Meligy/Code/github/Meligy/routing-angular-cli/src/app/app.module.ts

Expected behavior

I'd expect the AoT compiler to be able to understand

Minimal reproduction of the problem with instructions

  1. Create a new app using latest Angular CLI
  2. Go to AppModule, modify providers and add constructor as shown above
  3. Run npm start -- --aot

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

I want to be able to test the places where I happen to have to use window. I still want to have type safety for my calls to it.

Please tell us about your environment:

Mac OS latest

Running npm run ng -- version returns:

angular-cli: 1.0.0-beta.26
node: 6.9.1
os: darwin x64
@angular/common: 2.4.4
@angular/compiler: 2.4.4
@angular/core: 2.4.4
@angular/forms: 2.4.4
@angular/http: 2.4.4
@angular/platform-browser: 2.4.4
@angular/platform-browser-dynamic: 2.4.4
@angular/router: 3.4.4
@angular/compiler-cli: 2.4.4

Things to note:

  • The type Window is declared in default TypeScript lib dom.
  • The libs ["es6", "dom"] are already added by default in my project (default Angular CLI layout)
  • I also tried to reference the file node_modules/typescript/lib/lib.dom.d.ts directly in several ways, and that didn't seem to help

Please note that the code WORKS JUST FINE when I'm not using AoT.

That's npm start works, and npm start -- --aot fails (npm start here equals ng serve)

  • Angular version: 2.0.X

Angular 2.4.4 as shown above.

  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]

Tested in Chrome, but the error comes in Terminal anyway.

  • Language: [all | TypeScript X.X | ES6/7 | ES5]

TypeScript 2.0.10

  • Node (for AoT issues): node --version =
    node: 6.9.1

Activity

  1. DzmitryShylovich commented on Jan 21, 2017

    @DzmitryShylovich
    Contributor

    constructor(@Inject('window') w: Window)

    Window is an interface. #12631

  2. Meligy commented on Jan 21, 2017

    @Meligy
    ContributorAuthor

    @DzmitryShylovich That's why I'm using the string token not the type directly:

        {provide: 'window', useFactory: getWindow}
    

    Note the quotes.

  3. Meligy commented on Jan 21, 2017

    @Meligy
    ContributorAuthor

    Interesting. Looks like it happens even with strings and tokens.

  4. Meligy commented on Jan 21, 2017

    @Meligy
    ContributorAuthor

    You are absolutely right. This is a duplicate of #12631.

    I can workaround it by doing this funny way:

    @Injectable()
    export class WindowWrapper extends Window {
    
    }
    
    export function getWindow() { return window; }
    
    @NgModule({
      declarations: [
        AppComponent,
        SimpleRouteComponent
      ],
      imports: [
        BrowserModule,
        FormsModule,
        HttpModule,
        AppRoutingModule
      ],
      providers: [
        {provide: WindowWrapper, useFactory: getWindow}
      ],
      bootstrap: [AppComponent]
    })
    export class AppModule {
      constructor(w: WindowWrapper) {
        console.log(w);
      }
    }
    
  5. DzmitryShylovich commented on Jan 21, 2017

    @DzmitryShylovich
    Contributor

    it's not about the token type. it's about the argument type.

  6. Meligy commented on Jan 21, 2017

    @Meligy
    ContributorAuthor

    Yeah. I get it now. You are absolutely correct.

  7. angular-automatic-lock-bot commented on Sep 10, 2019

    @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.

  8. locked and limited conversation to collaborators on Sep 10, 2019
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions