Created
June 11, 2024 17:31
-
-
Save guiseek/11fb549122f2e0c443e033b6146b43d3 to your computer and use it in GitHub Desktop.
Define typed custom element
This file contains 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
type Tag = `${string}-${string}`; | |
type TagMap = HTMLElementTagNameMap; | |
interface CustomElement<T> extends CustomElementConstructor { | |
new (...params: any[]): T; | |
} | |
function define<S extends Tag, K extends keyof TagMap>(selector: S, is?: K) { | |
return (target: CustomElement<TagMap[K]>) => { | |
customElements.define(selector, target, is ? { extends: is } : {}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment