7

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?

3
  • Not using jquery? You are using angular Commented May 11, 2018 at 23:27
  • I concur. You shouldn't have to use jQuery click events in an Angular setting, unless there's some jQuery-specific functionality you need to use for legacy reasons? Commented May 11, 2018 at 23:51
  • @kingdaro We're dynamically generating a Datatables.net DataTable - how do I use Angular to have an on click event in that case? Commented May 14, 2018 at 17:34

1 Answer 1

21

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); });

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.