The @customElement decorator is shorthand for calling customElements.define, which registers a custom element class with the browser and associates it with an element name (in this case, simple-greeting).
If you're using JavaScript, or if you're not using decorators, you can call define() directly:
TypeScript will infer the class of an HTML element returned from certain DOM APIs based on the tag name. For example, document.createElement('img') returns an HTMLImageElement instance with a src: string property.
Custom elements can get this same treatment by adding to the HTMLElementTagNameMap as follows:
@customElement('my-element')
exportclassMyElementextendsLitElement {
@property({type: Number})
aNumber: number=5;
/* ... */
}
declareglobal {
interfaceHTMLElementTagNameMap {
"my-element": MyElement;
}
}
By doing this, the following code properly type-checks:
We recommend adding an HTMLElementTagNameMap entry for all elements authored in TypeScript, and ensuring you publish your .d.ts typings in your npm package.