Skip to content

Unexpectedly call getter/setter when subclassing #36976

Closed
@unadlib

Description

@unadlib

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions