Skip to content

Unexpectedly call getter/setter when subclassing #36976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
unadlib opened this issue Feb 24, 2020 · 3 comments
Closed

Unexpectedly call getter/setter when subclassing #36976

unadlib opened this issue Feb 24, 2020 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@unadlib
Copy link

unadlib commented Feb 24, 2020

TypeScript Version: 3.7.x-dev.201xxxxx

Search Terms:
getter/setter, subclassing

Code

const x: any = []
class A {
    a() { 
        x.push(this);
        return this;
    }

    get s() { 
        x.push(this);
        return this;
    }

    set s(n: any) { 
        x.push(this);
    }
}

class B extends A {
    a() {
        x.push(this);
        return super.a();
    }

    get s() { 
        x.push(this);
        // @ts-ignore
        return super.s;
    }

    set s(n: any) {
        x.push(this);
        // @ts-ignore
        super.s = n;
    }
}

const b = new B(); b.a(); b.s; b.s = 1;
console.log([x[0] === x[1], x[2] === x[3], x[4] === x[5]]);

ts compiler:Only public and protected methods of the base class are accessible via the 'super' keyword.ts(2340) and // @ts-ignore.

Output
"use strict";
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 (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var x = [];
var A = /** @class */ (function () {
    function A() {
    }
    A.prototype.a = function () {
        x.push(this);
        return this;
    };
    Object.defineProperty(A.prototype, "s", {
        get: function () {
            x.push(this);
            return this;
        },
        set: function (n) {
            x.push(this);
        },
        enumerable: true,
        configurable: true
    });
    return A;
}());
var B = /** @class */ (function (_super) {
    __extends(B, _super);
    function B() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    B.prototype.a = function () {
        x.push(this);
        return _super.prototype.a.call(this);
    };
    Object.defineProperty(B.prototype, "s", {
        get: function () {
            x.push(this);
            // @ts-ignore
            return _super.prototype.s;
        },
        set: function (n) {
            x.push(this);
            // @ts-ignore
            _super.prototype.s = n;
        },
        enumerable: true,
        configurable: true
    });
    return B;
}(A));
var b = new B();
b.a();
b.s;
b.s = 1;
console.log([x[0] === x[1], x[2] === x[3], x[4] === x[5]]);
Compiler Options
{
  "compilerOptions": {
    "target": "es5"
  }
}

Expected behavior:
[ true, true, true ]

Actual behavior:
[ true, false, false ]

Playground Link:

Related Issues:
Allow to call getter/setter logic When subclassing

@tadhgmister
Copy link

Can't reproduce on 3.7.5 playground: Playground Link

@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Feb 24, 2020

Duplicate #338; using getters/setters via super is not supported in target: ES5. The code runs as expected in ES6+ target

The errors you're ignoring are the errors which are telling you that this won't work...

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 24, 2020
@unadlib
Copy link
Author

unadlib commented Feb 25, 2020

Got it. so sad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants