Skip to content

Bug or breaking change in super constructors? #12669

Closed
@Elephant-Vessel

Description

@Elephant-Vessel

TypeScript Version: 2.1.1 and nightly (2.2.0-dev.20161205)

Code

abstract class Animal
{
    constructor(someStr) { }

    public GetName() {
        return "test";
    }
}

class Dog extends Animal
{
    constructor()
    {
        super(super.GetName());
        var test = super.GetName();
    }
}

Expected behavior: (As per 2.0.10 and Playground)

var Dog = (function (_super) {
    __extends(Dog, _super);
    function Dog() {
        _super.call(this, _super.prototype.GetName.call(this)); // <= Look here
        var test = _super.prototype.GetName.call(this);
    }
    return Dog;
}(Animal));

Actual behavior:

var Dog = (function (_super) {
    __extends(Dog, _super);
    function Dog() {
        var _this = _super.call(this, _super.GetName.call(_this)) || this; // <= Look here
        var test = _super.prototype.GetName.call(_this);
        return _this;
    }
    return Dog;
}(Animal));

Notice the difference in the compiled call to _super.GetName/_super.prototype.GetName when super.GetName occurs in the super-constructor, while calls to _super.GetName outside of the constructor always gets compiled to _super.prototype.GetName.

GetName is always attached to the prototype, so the _super.GetName is breaking runtime.

Is there an intention, which would make sense, to disallow calls to super until after the constructor is run, only allowing static calls? Nevertheless, the generated code don't make sense and no compiler error is shown.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions