From 723c588fef6aba9f10ea9f5ea7bc444532519f9a Mon Sep 17 00:00:00 2001 From: xobotyi Date: Sun, 31 Jan 2021 03:03:51 +0300 Subject: [PATCH] feat: improve `on` and `off` util functions typing. --- src/misc/util.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/misc/util.ts b/src/misc/util.ts index 96e5ffaf9b..a8d5455202 100644 --- a/src/misc/util.ts +++ b/src/misc/util.ts @@ -1,7 +1,21 @@ export const noop = () => {}; -export const on = (obj: any, ...args: any[]) => obj.addEventListener(...args); +export function on( + obj: T | null, + ...args: Parameters | [string, Function | null, ...any] +): void { + if (obj && obj.addEventListener) { + obj.addEventListener(...(args as Parameters)); + } +} -export const off = (obj: any, ...args: any[]) => obj.removeEventListener(...args); +export function off( + obj: T | null, + ...args: Parameters | [string, Function | null, ...any] +): void { + if (obj && obj.removeEventListener) { + obj.removeEventListener(...(args as Parameters)); + } +} export const isBrowser = typeof window === 'object';