feat: improve on and off util functions typing.

This commit is contained in:
xobotyi 2021-01-31 03:03:51 +03:00
parent a27f09fd36
commit 723c588fef
No known key found for this signature in database
GPG Key ID: 36FA5C1DA5FEB62D

View File

@ -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';