Last active
September 4, 2025 00:56
-
-
Save cecilemuller/72fbb3bc3a77d82c8a969cdfaa06508c to your computer and use it in GitHub Desktop.
Typescript: augment Custom Elements registry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It also adds the components to autocompletion Intellisense: