Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript jQuery on click event type

Tags:

typescript

I have some code in an angular typescript file:

$('.dashboard-table').on('click', 'tbody tr', 
      function(event: JQueryEventObject) {   // changing this to any removes TS error
          const test: Test = <Test>$('.dashboard-table').DataTable().row(this).data();
          self.openTest(test.id, event);
      }
);

openTest(id: number, $event: MouseEvent|JQueryEventObject) {
    let target = $event.srcElement || $event.target as HTMLElement;
    if (target.tagName.toLowerCase() == 'a'
        || $(target).parents('a').length
        || target.tagName.toLowerCase() == 'button'
        || $(target).parents('button').length) {
        return;
    }
    this.router.navigate([this.urls.paths.test(id)]);
}

It's throwing a TypeScript error:

error TS2345: Argument of type '"click"' is not assignable to parameter of type 
'PlainObject<false | EventHandler<HTMLElement, 
(this: HTMLElement, event: JQueryEventObject) => vo...'.`.  

How do I get ng serve to not truncate the TypeScript error message? How do I re-write it to not fail typescript checking?

like image 742
Denise Mauldin Avatar asked Dec 06 '22 11:12

Denise Mauldin


1 Answers

JQuery's on() method passes an object of type JQuery.Event to the handler, which you can read more about here: https://api.jquery.com/category/events/event-object/.

Example: $(document).on('click', (event: JQuery.Event) => { console.log(event); });

like image 80
Sean Avatar answered Dec 09 '22 15:12

Sean