Use returned values from super calls as ‘this’

In ES2015, constructors which return an object implicitly substitute the value of this for any callers of super().As a result, it is necessary to capture any potential return value of super() and replace it with this.This change enables working with Custom Elements, which takes advantage of this to initialize browser-allocated elements with user-written constructors.

Example
  1. class Base {
  2. x: number;
  3. constructor() {
  4. // return a new object other than `this`
  5. return {
  6. x: 1,
  7. };
  8. }
  9. }
  10. class Derived extends Base {
  11. constructor() {
  12. super();
  13. this.x = 2;
  14. }
  15. }

Generates:

  1. var Derived = (function (_super) {
  2. __extends(Derived, _super);
  3. function Derived() {
  4. var _this = _super.call(this) || this;
  5. _this.x = 2;
  6. return _this;
  7. }
  8. return Derived;
  9. }(Base));

This change entails a break in the behavior of extending built-in classes like Error, Array, Map, etc.. Please see the extending built-ins breaking change documentation for more details.