Last active
April 10, 2024 23:00
-
-
Save trvswgnr/b4f6ac7b6e64a21d4e7eaed54c8e5476 to your computer and use it in GitHub Desktop.
functional programming typescript extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type AnyFn = (...args: any[]) => any; | |
namespace ElementExt { | |
export function addEventListener< | |
El extends Element | Document, | |
const S extends string, | |
const H extends AnyFn, | |
>(name: S, cb: H): (el: El) => void; | |
export function addEventListener< | |
El extends Element | Document, | |
const S extends string, | |
const H extends AnyFn, | |
>(el: El, name: S, cb: H): void; | |
export function addEventListener( | |
arg0: string | (Element | Document), | |
arg1: string | AnyFn, | |
arg2?: EventListenerOrEventListenerObject, | |
) { | |
if (typeof arg0 === "string" && typeof arg1 === "function") { | |
return (el: Element) => el.addEventListener(arg0, arg1); | |
} | |
if ((arg0 instanceof Element || arg0 === document) && typeof arg1 === "string" && typeof arg2 === "function") { | |
return arg0.addEventListener(arg1, arg2); | |
} | |
throw new Error("invalid arguments"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment