As per the discussion here, I have had to extend the window object in order to strongly type Intercom's window functionality.
Originally the code used to look like this:
setCurrentRoute() {
if (
this.currentRoute.getValue().indexOf('website') > -1 ||
this.currentRoute.getValue().indexOf('builder') > -1
) {
(window as any).Intercom('update', {
hide_default_launcher: true,
});
} else {
(window as any).Intercom('update', {
hide_default_launcher: false,
});
}
localStorage.setItem('currentRoute', this.currentRoute.getValue());
}
But as I am trying to leverage more of Typescript's type safety features, it now looks like this:
declare global {
interface Window {
Intercom(update: string, params: { hide_default_launcher: boolean }): void;
}
}
setCurrentRoute(): void {
if (this.currentRoute.getValue().indexOf('website') > -1 || this.currentRoute.getValue().indexOf('builder') > -1) {
this.window.Intercom('update', {
'hide_default_launcher': true
});
} else {
this.window.Intercom('update', {
'hide_default_launcher': false
});
}
localStorage.setItem('currentRoute', this.currentRoute.getValue());
}
However, I get the following error message:
ERROR TypeError: Cannot read property 'Intercom' of undefined
at RouterService.push../src/app/shared/services/router.service.ts.RouterService.setCurrentRoute
How do I fix it?