简体   繁体   中英

How to fix Typescript HTMLElement constructor: 'new' is required

I'm making loop custom element with TS.
As I make Loop class, it compile to function and caught error at the _super.call(this) inside of var Loop

My Typescript file

class Loop extends HTMLElement {
  constructor() {
    super();
    const a = document.createElement('div');
    a.innerText = 'a';
    this.appendChild(a);
  }
}

customElements.define('loop-', Loop);

Outfile

var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var Loop = /** @class */ (function (_super) {
    __extends(Loop, _super);
    function Loop() {
        var _this = _super.call(this) || this;
        var a = document.createElement('div');
        a.innerText = 'a';
        _this.appendChild(a);
        return _this;
    }
    return Loop;
}(HTMLElement));
customElements.define('loop-', Loop);

Error

Uncaught TypeError: HTMLElement constructor: 'new' is required

Use the following command to run the typescript code

tsc filename.ts  --target es6 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM