Mark Sandman
Mark Sandman

Reputation: 3333

Angular2 Error - Property 'innerWidth' does not exist on type 'EventTarget'

In my Angular2 app I get the following error:

 app/src/main.ts(104,42): error TS2339: Property 'innerWidth' does not exist on type 'EventTarget'.

I have the following method in my component and the compiler doesn't like the innerWidth property of my event object.

onWindowResize(event: Event):void {
        if (event.target && event.target.innerWidth >= 768) {
            this.showMenu = false;
        }
    }

What do I need to do to prevent / overcome this error? I thought that the first check of if (event.target would prevent such a crash. Also the error only occurred when I gave the event argument the type definition of Event. Any help would be appreciated.

Upvotes: 1

Views: 1986

Answers (1)

yurzui
yurzui

Reputation: 214245

You can override Event class like this:

interface MyEventTarget extends EventTarget {
    innerWidth: number
}

interface MyEvent extends Event {
    target: MyEventTarget;
}

Then your code is changed to:

onWindowResize(event: MyEvent):void { <== new created class
  if (event.target && event.target.innerWidth >= 768) {
     this.showMenu = false;
  }
}

Upvotes: 3

Related Questions