I had the idea of enhancing all interfaces in HTMLElementTagNameMap with chained functionality. Since there are numerous interfaces, and all elements either are HTMLElement or extend it, I wanted a way to achieve something like this:
interface HTMLElement {
doSomething<K extends keyof HTMLElementTagNameMap>(anArgument: any): HTMLElementTagNameMap[K]
}
Unfortunately, this approach doesn't work as expected. This is because every interface that extends HTMLElement now includes a doSomething method that should return any value from HTMLElementTagNameMap. What I actually want is for HTMLTableElement.doSomething to return an HTMLTableElement object, and HTMLDivElement.doSomething to return an HTMLDivElement object, and so forth.
Can such generalization be accomplished, or do I need to repeat this process for each individual interface?
If it is possible, how can I go about implementing it?