I am trying to play with CustomElements in JavaScript without a framework, for educational purposes. I am trying to write a base class for my CustomElements, for loading a template and setting up a ShadowDOM. Since the template for a CustomElement depends on the actual implementation, I found myself wanting "abstract static properties".
abstract class CustomElement extends HTMLElement {
abstract static template: HTMLTemplateElement;
constructor() {
super();
this.shadow_root = this.attachShadow({ "mode": "open" });
this.shadow_root.appendChild(this.constructor.template.content.cloneNode(true)); // How can can I tell typescript, that the constructor does in fact have a template property?
}
}
class CustomCircle extends CustomElement {
static override template = loadTemplate(`CircleTemplate`); // I want this to be static (only do the loading work once, and enforce that everyone implementing "CustomElement" has it
constructor() {
super();
...
}
}
class CustomRectangle extends CustomElement {
static override template = loadTemplate(`RectangleTemplate`);
constructor() {
super();
...
}
}
Now abstract static is not supported in TypeScript, what is my best shot at expressing every class extending CustomElement must have a (static) template property?
(Extra: how do I tell TypeScript that this.constructor is guaranteed to have the template property?)
abstractat all. Maybe you should replace "JavaScript" with "TypeScript" here since that's the context in whichabstractmakes sense?