@@ -11,7 +11,7 @@ import { useMemoizedFunction } from ".";
1111 * @param {"normal"|"layout" } [options.effectType="normal"] - option to set which hook is used to attach event listener.
1212 * @returns {()=>void } remove - used to manually remove the eventListener
1313 */
14- export const useEventListener = ( { type, listener, element = window , listenerOpts, effectType= "normal" } : { type : string , listener : ( evt : Event | CustomEvent ) => void , element ?: RefObject < HTMLElement > | Window , listenerOpts ?: boolean | AddEventListenerOptions , effectType ?: "normal" | "layout" } ) => {
14+ export const useEventListener = < T extends Event | CustomEvent > ( { type, listener, element = window , listenerOpts, effectType= "normal" } : { type : string , listener : ( ( evt : T ) => unknown | Promise < unknown > ) , element ?: RefObject < HTMLElement > | Window , listenerOpts ?: boolean | AddEventListenerOptions , effectType ?: "normal" | "layout" } ) => {
1515 const optsMemoized = useRef < typeof listenerOpts > ( listenerOpts ) ;
1616 const elementReference = useRef < HTMLElement | Window | null > ( ) ;
1717 const effect = effectType === "layout" ? useLayoutEffect : useEffect ;
@@ -24,14 +24,14 @@ export const useEventListener = ({ type, listener, element = window, listenerOpt
2424 : null
2525 : element as Window
2626
27- elementReference . current && ( elementReference . current as HTMLElement | Window ) . addEventListener ( type , listener , opts ) ;
27+ elementReference . current && ( elementReference . current as HTMLElement | Window ) . addEventListener ( type , listener as EventListenerOrEventListenerObject , opts ) ;
2828 return ( ) => {
29- elementReference . current && ( elementReference . current as HTMLElement | Window ) . removeEventListener ( type , listener , opts ) ;
29+ elementReference . current && ( elementReference . current as HTMLElement | Window ) . removeEventListener ( type , listener as EventListenerOrEventListenerObject , opts ) ;
3030 }
3131 } , [ element , type , listener ] ) ;
3232
3333 const remove = useMemoizedFunction ( ( ) => {
34- elementReference . current && ( elementReference . current as HTMLElement | Window ) . removeEventListener ( type , listener , optsMemoized . current ) ;
34+ elementReference . current && ( elementReference . current as HTMLElement | Window ) . removeEventListener ( type , listener as EventListenerOrEventListenerObject , optsMemoized . current ) ;
3535 } ) ;
3636
3737 return remove ;
0 commit comments