I'have the following error even if the code run perfectly:
"TS2345: Argument of type 'Event' is not assignable to parameter of type 'KeyboardEvent'.
Property 'altKey' is missing in type 'Event'."
// In a Class
public listenTo = (window: Window) => {
['keydown', 'keyup'].forEach(eventName => {
window.addEventListener(eventName, e => {
this.handleEvent(e); // <- error here
});
});
}
public handleEvent = (event: KeyboardEvent) => {
const { key } = event;
//...
}
I tried to define the event type to KeyboardEvent, but I have the following error :
window.addEventListener(eventName, (e:KeyboardEvent) => {
this.handleEvent(e); // <- error here
});
TS2345: Argument of type '(event: KeyboardEvent) => void' is not assignable to parameter of type 'EventListenerOrEventListenerObject'.
Type '(event: KeyboardEvent) => void' is not assignable to type 'EventListenerObject'.
Property 'handleEvent' is missing in type '(event: KeyboardEvent) => void'.
Is there a way to pass through or resolve the issue ?