[WICG/webcomponents] [custom-elements] Proposal for definition-time validation of dependencies (Issue #988)

This proposal introduces the `requiredElements` static prop to add definition-time validation of custom element dependencies.

There is a precedent for static property used during definition with `observedAttributes`.

```js
class ComponentA extends HTMLElement {}

// ComponentB requires ComponentA
class ComponentB extends HTMLElement {
  static requiredElements = [ComponentA];
  connectedCallback() { this.appendChild(new ComponentA()); } 
}

// ComponentB requires ComponentB, but also ComponentA through ComponentB
class ComponentC extends HTMLElement {
  static requiredElements = [ComponentB];
  connectedCallback() { this.appendChild(new ComponentB()); } 
}

// This would throw an error complaining that ComponentB and ComponentA have to be defined.
customElements.define('component-c', ComponentC);
```

Tools like eslint could be used to ensure that every elements used have been added to `requiredElements`.


-- 
Reply to this email directly or view it on GitHub:
https://github.com/WICG/webcomponents/issues/988
You are receiving this because you are subscribed to this thread.

Message ID: <WICG/webcomponents/issues/988@github.com>

Received on Thursday, 9 March 2023 12:36:54 UTC