From b83e507ddab4af1a9f0d5dc01009d03a63a8fbdc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ulrich-Matthias=20Sch=C3=A4fer?= <ulima.ums@googlemail.com>
Date: Tue, 16 Jul 2024 15:57:35 +0200
Subject: [PATCH] fix(types): make on/off generic

---
 svg.js.d.ts | 58 +++++++++++++++++++++++++++++++++--------------------
 1 file changed, 36 insertions(+), 22 deletions(-)

diff --git a/svg.js.d.ts b/svg.js.d.ts
index 5fd0513e..f337c7c8 100644
--- a/svg.js.d.ts
+++ b/svg.js.d.ts
@@ -16,6 +16,8 @@ interface CSSStyleDeclarationWithVars extends CSSStyleDeclaration {
 }
 
 declare module '@svgdotjs/svg.js' {
+  export interface SVGDotJsEventMap extends HTMLElementEventMap {}
+  
   function SVG(): Svg
   /**
    * @param selectorOrHtml pass in a css selector or an html/svg string
@@ -35,31 +37,32 @@ declare module '@svgdotjs/svg.js' {
   function prepare(element: HTMLElement): void
   function getClass(name: string): Element
 
-  function on(
-    el: Node | Window,
-    events: string,
-    cb: EventListener,
-    binbind?: any,
+  function on<K extends keyof SVGDotJsEventMap, El extends Node | Window>(
+    el: El,
+    event: K,
+    cb: (this: El, ev: SVGDotJsEventMap[K]) => any,
     options?: AddEventListenerOptions
-  ): void
-  function on(
-    el: Node | Window,
-    events: Event[],
-    cb: EventListener,
+  ): void;
+
+  function on<El extends Node | Window>(
+    el: El,
+    events: string | string[],
+    cb: (this: El, ev: Event) => any,
     binbind?: any,
     options?: AddEventListenerOptions
   ): void
 
-  function off(
-    el: Node | Window,
-    events?: string,
-    cb?: EventListener | number,
+  function off<K extends keyof SVGDotJsEventMap, El extends Node | Window>(
+    el: El,
+    events?: K,
+    cb?: (this: El, ev: SVGDotJsEventMap[K]) => any,
     options?: AddEventListenerOptions
   ): void
-  function off(
-    el: Node | Window,
-    events?: Event[],
-    cb?: EventListener | number,
+
+  function off<El extends Node | Window>(
+    el: El,
+    events?: string | string[],
+    cb?: (this: El, ev: any) => any,
     options?: AddEventListenerOptions
   ): void
 
@@ -692,15 +695,26 @@ declare module '@svgdotjs/svg.js' {
     getEventHolder(): this | Node
     getEventTarget(): this | Node
 
+    on<K extends keyof SVGDotJsEventMap>(
+      events: K,
+      cb: (ev: SVGDotJsEventMap[K]) => any,
+      binbind?: any,
+      options?: AddEventListenerOptions
+    ): this
     on(
-      events: string | Event[],
-      cb: EventListener,
+      events: string | string[],
+      cb: (ev: Event) => any,
       binbind?: any,
       options?: AddEventListenerOptions
     ): this
+    off<K extends keyof SVGDotJsEventMap>(
+      events?: K,
+      cb?: (ev: any) => any,
+      options?: AddEventListenerOptions
+    ): this
     off(
-      events?: string | Event[],
-      cb?: EventListener | number,
+      events?: string | string[],
+      cb?: (ev: any) => any,
       options?: AddEventListenerOptions
     ): this