TypeScript 2.1 Apparently now has support for writing custom html elements ("What's new in TypeScript - 2.1")
However I have been unable to find any documentation on how this is supposed to work.
Can anyone provide an explanation on how this is supposed to work, ideally with examples?
Thank You
Currently this is not possible, when targeting ES5 with TypeScript. The Custom Elements API V1 needs ES6/ES2015-style classes. However if you target ES5 with TypeScript (for compatibility with IE 11 for example) all classes get transpiled to functions.
This is not a TypeScript limitation, but a limitation of the Custom Elements API V1 itself.
You have two options:
The release notes for TypeScript 2.1 are misleading; it is not a TypeScript issue at all. See this issue for more background.
The only thing I can find on that page about custom elements is that the new way of handling super()
calls in ES5 allows TypeScript to be used to define custom elements.
This just means that you can now write regular ES2015 code to define a custom element, and the new TypeScript compiler will output the correct ES5 code for it. Previously, the outputted code would not do the right thing with the super()
call, which would break custom element classes.
There are no TypeScript examples for this, since this isn't really specific to TypeScript. It's just following the custom elements standard:
class MyCustomElement extends HTMLElement {
constructor() {
super();
this.foo = "bar";
}
doSomething() {
console.log(this.foo);
}
}
customElements.define("my-custom-element", MyCustomElement);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With