angulareventstypescriptevent-listenerangular2-directives

Property 'clientX' does not exist on type 'Event'. Angular2 Directive


I'm trying to listen to the X position of my mouse in an Angular2 Directive like this:

@HostListener('mousemove', ['$event'])
onMousemove(event: Event): void  { 
  console.log(event.clientX)
}

but I get the error

Property 'clientX' does not exist on type 'Event'.

This is odd because this listener

@HostListener('mousemove', ['$event'])
onMousemove(event: Event): void  { 
  console.log(event)
}

logs out an event object

enter image description here

Why can I not access event.clientX?


Solution

  • Change the parameter type

    onMousemove(event: MouseEvent): void  { 
      console.log(event.clientX)
    }