Skip to content

Commit

Permalink
feat: improve on and off util functions typing.
Browse files Browse the repository at this point in the history
  • Loading branch information
xobotyi committed Jan 31, 2021
1 parent a27f09f commit 723c588
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/misc/util.ts
@@ -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';

0 comments on commit 723c588

Please sign in to comment.