Here is my typescript file onKey(event:any) { c...">

How to find event type in Typescript

Here is my template

<input (keyup)="onKey($event)">

Here is my typescript file

onKey(event:any) {
  console.log(typeof event);
}

console.logdeduces object, but actually it should be KeyboardEvent.

Is there a general way to search for an event type?

+4
source share
1 answer

You probably want to just check event.typeto find out what it is and infer the type from it.

Otherwise, you can try using event instanceof KeyboardEventeither custom types of protection .

Also, in your example, you can just make an argument event:KeyboardEventinstead event:any.

+6
source

All Articles