我在角度打字稿文件中有一些代码:
$('.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)]);
}
它引发了TypeScript错误:
error TS2345: Argument of type '"click"' is not assignable to parameter of type
'PlainObject<false | EventHandler<HTMLElement,
(this: HTMLElement, event: JQueryEventObject) => vo...'.`.
如何让ng serve
不截断TypeScript错误消息?如何将其重写为不失败的打字稿检查?
答案 0 :(得分:6)
JQuery的on()方法将类型为JQuery.Event的对象传递给处理程序,您可以在这里阅读更多信息:https://api.jquery.com/category/events/event-object/。
实施例:
$(document).on('click', (event: JQuery.Event) => { console.log(event); });