gevik
gevik

Reputation: 3407

Why calls to super() in constructor returns void

Is there a particular reason why calls to the super() in the constructor returns void instead of the type of the same class?

Source: TypeScript specs 4.9.1

Example: See example here

Upvotes: 1

Views: 178

Answers (2)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249546

I am not able to find the documentation, but if the super call returns a different this then this new value will become this for the rest of the call. This is the behavior mandated by the ES 2015 specification. If you look at the generated code, you will see that the return value of super is used as this in the rest of the constructor:

function RoundButton(config) {
    // _this will either be the current this or whatever is returned by _super.call
    var _this = _super.call(this, config) || this;
    // refercens to this are replaces with _this
    _this.config.text = '....'; // <=== Property 'config' does not exist on type 'void'.
    return _this;
}

Since this behavior is already mandated, you are not free to do as you wish with the return value of super, so this is probably why it returns void in the type system.

Upvotes: 2

Emil Larsson
Emil Larsson

Reputation: 226

Why would you want it that way?

Isn't it even easier and more readable the way that it is?

var me = super(config);
me.config.text = '....'; // <=== Property 'config' does not exist on type 'void'.

super(config);
config.text = '....'; // <=== Easier?

Upvotes: 0

Related Questions