mirror of
https://github.com/streamich/react-use.git
synced 2026-01-25 14:17:16 +00:00
feat: improve on and off util functions typing.
This commit is contained in:
parent
a27f09fd36
commit
723c588fef
@ -1,7 +1,21 @@
|
||||
export const noop = () => {};
|
||||
|
||||
export const on = (obj: any, ...args: any[]) => obj.addEventListener(...args);
|
||||
export function on<T extends Window | Document | HTMLElement | EventTarget>(
|
||||
obj: T | null,
|
||||
...args: Parameters<T['addEventListener']> | [string, Function | null, ...any]
|
||||
): void {
|
||||
if (obj && obj.addEventListener) {
|
||||
obj.addEventListener(...(args as Parameters<HTMLElement['addEventListener']>));
|
||||
}
|
||||
}
|
||||
|
||||
export const off = (obj: any, ...args: any[]) => obj.removeEventListener(...args);
|
||||
export function off<T extends Window | Document | HTMLElement | EventTarget>(
|
||||
obj: T | null,
|
||||
...args: Parameters<T['removeEventListener']> | [string, Function | null, ...any]
|
||||
): void {
|
||||
if (obj && obj.removeEventListener) {
|
||||
obj.removeEventListener(...(args as Parameters<HTMLElement['removeEventListener']>));
|
||||
}
|
||||
}
|
||||
|
||||
export const isBrowser = typeof window === 'object';
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user