Home > Net >  angular strict mode throws and error as `Element implicitly has an 'any' type because inde
angular strict mode throws and error as `Element implicitly has an 'any' type because inde

Time:07-29

code :

platformBrowserDynamic().bootstrapModule(AppModule).then(ref => {

  if (window['ngRef']) {
    window['ngRef'].destroy();
  }
  window['ngRef'] = ref;

 
}).catch(err => console.error(err));

the above code form main.ts file from angular. how to fix in this case? what is the correct way to update this code?

CodePudding user response:

Change it to any type

const _window = window as any;
if (_window['ngRef']) {
  _window.destroy();
}
_window['ngRef'] = ref;

The above code is used by webpack for hot reloads to ensure Angular destroys itself on hot reloads.

Read about the typescript error here

BTW if you don't want to use any, but want to use strict type instead, then you can extend window's type

//This will tell typescript that window also contains ngRef property
type windowExtended = Window & typeof globalThis & {
  ngRef: NgModuleRef<AppModule>;
}

platformBrowserDynamic().bootstrapModule(AppModule)
  .then(ref => {
    const _window = window as windowExtended;
    if (_window['ngRef']) {
      _window['ngRef'].destroy();
    }
    _window['ngRef'] = ref;

  })
  • Related