Skip to content

Instantly share code, notes, and snippets.

@cecilemuller
Last active September 4, 2025 00:56
Show Gist options
  • Save cecilemuller/72fbb3bc3a77d82c8a969cdfaa06508c to your computer and use it in GitHub Desktop.
Save cecilemuller/72fbb3bc3a77d82c8a969cdfaa06508c to your computer and use it in GitHub Desktop.
Typescript: augment Custom Elements registry
// Exposes a Web Component that is registered using:
// customElements.define("x-example", ExampleElement)
declare global {
interface CustomElementRegistry {
get(name: "x-example"): typeof ExampleElement | undefined;
}
}
// No need to import the type or typecast the constructor anymore,
// Intellisense recognizes its properties and methods
const ExampleElement = customElements.get("x-example");
if (ExampleElement) {
const element = new ExampleElement();
console.log(element.myprop);
document.body.appendChild(element);
}
@cecilemuller
Copy link
Author

cecilemuller commented Sep 4, 2025

It also adds the components to autocompletion Intellisense:

VSCode autocompletes the component names Autocomplete shows the documentation Define the documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment