Skip to content

How to document custom elements without too many hacks #2844

@hesxenon

Description

@hesxenon

Search terms

global, export

Question

So I tried to find a way to document what is essentially a big "mess" around globals.

As a CE author I want to be able to document the usage of my CE without changing the implementation.

Ideally what I would like to do is the following:

// not exported on purpose, should only be available on global object as importing this would
// throw an "ce already defined" error
namespace Module {
  export class MyCE extends HTMLElement {}
  customElements.define("my-ce", MyCE); // side effect!

  export namespace MyCE {
    // types and utils specific to my custom element
  }
}

declare global {
  namespace globalThis {
    // Apparently the only way to re-export a symbol without changing its type explicitly?
    // other approaches include declaring the `typeof class` as a `var`, `let` or `const` or some
    // variation thereof
    /**
     * documentation about the class itself
     */
    export import MyCE = Module.MyCE;
  }

  interface HTMLElementTagNameMap {
    /**
     * documentation about the element
     */
    "my-ce": Module.MyCE
  }
}
Object.assign(globalThis, Module);

Do you see this happening in the future? Or should I rather change the doc tool here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionQuestion about functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions