Skip to content

Wrong constructor code generated when target is ES5 #48761

Closed
@p-szm

Description

@p-szm

Bug Report

When this code is compiled to ES5 on nightly (4.7.0-dev.20220419)

class A {
    public constructor() {
        console.log("A")
    }
}

class B extends A  {
    constructor() {
        "ngInject";
        console.log("B")
        super();
    }
}

const b = new B();

it results with:

"use strict";
var __extends = (this && this.__extends) || (function () {
    /* implementation omitted for brevity */
})();
var A = (function () {
    function A() {
        console.log("A");
    }
    return A;
}());
var B = (function (_super) {
    __extends(B, _super);
    function B() {
        "ngInject";
        console.log("B");
        _this = _super.call(this) || this;
        return _super.call(this) || this;
    }
    return B;
}(A));
var b = new B();

In addition to _super.call(this) being duplicated, this code also fails at runtime because _this is not defined.

🔎 Search Terms

constructor, super, ngInject, directive, _this, this

🕗 Version & Regression Information

  • This changed between versions 4.5.x and 4.6.x

I expect #29374 to have introduced this regression. The code compiles and runs correctly on 4.5.x.

#48671 looks semi-related and it's already been fixed, but my issue seems a bit different.

⏯ Playground Link

https://www.typescriptlang.org/play?downlevelIteration=true&removeComments=true&target=1&ts=4.7.0-dev.20220419#code/MYGwhgzhAECC0G8BQBIADgVwEYgJbGmAHsA7CAFwCcNhyjKAKASkSWnY-eLKJAFMAdCCIBzBgCJY4pmw4BfJAqShIMAELQ+AD3J8SAExjxWnbhWq16zE5w7iSIgJIkAVn1riA3LNuFSEXkFhMXE1aR9bCAw0PkYmb04FJTNyaCxoAF5oEj4Ad2g1Zm8gA

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

    Participants

    @DanielRosenwasser@jakebailey@RyanCavanaugh@p-szm@typescript-bot

    Issue actions

      Wrong constructor code generated when target is ES5 · Issue #48761 · microsoft/TypeScript