Answer in Brief
The type definition for EventListener
is as follows:
interface EventListener {
(evt: Event): void;
}
It requires an Event as the first argument, not a string
. Therefore, your event handler should be able to accept an Event and extract necessary information from it to set the locked state.
In-depth Explanation
Lets troubleshoot the issue step by step.
The signature for window.addEventListener
is defined as:
interface Window extends EventTarget {
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
}
You have passed the string
value unlockProtocol
as the first argument. Since this does not match any key on WindowEventMap
, it defaults to the second overload where the listener should match
EventListenerOrEventListenerObject
.
Your handler has been inferred as (e: string) => void
, which needs to comply with
EventListenerOrEventListenerObject
whose type is either
EventListener
or
EventListenerObject
.
Hence, the error arises because the function expects an Event as the first argument according to EventListener
's signature. Modify your event handler to handle an Event and use the necessary data to manage your application's locked state.