I am working with a base class that is abstract and extends HTMLElement. The base class calls CustomElementRegistry.define. I have encountered an issue where Typescript does not approve of using "this" as the second argument due to the abstract nature of the base class. However, it doesn't make sense because the final class will never be abstract, and it seems that "this" belongs to MyClass in the example. To work around this, I had to resort to using a rather ugly cast. Despite this workaround, the code (in its entirety) functions correctly. Is there a more elegant solution to this problem?
P.S. I apologize for providing an incorrect example earlier, rendering the first three responses irrelevant.
abstract class CustomElement extends HTMLElement {
constructor() {
super();
}
static register(name: string) {
customElements.define(
name,
this, // This argument triggers an error.
{});
}
}
class MyElement extends CustomElement {
constructor() {
super();
}
}
const e = new MyElement();
MyElement.register("my-element");