Skip to content

Incorrect class constructor #11846

Closed
@falsandtru

Description

@falsandtru

TypeScript Version: master

Code

const cache = new Map<string, S>();

class S {
    constructor(name = '') {
        return cache.has(name)
            ? cache.get(name)!
            : cache.set(name, this).get(name)!;
    }
}

const cached = new WeakSet<C>();

class C extends S {
    constructor(name = '') {
        super(name);
        if (cached.has(this)) return;
        cached.add(this);
    }
}
console.log(new C() === new C()); // true

Expected behavior:

var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var cache = new Map();
var S = (function () {
    function S(name) {
        if (name === void 0) { name = ''; }
        return cache.has(name)
            ? cache.get(name)
            : cache.set(name, this).get(name);
    }
    return S;
}());
var cached = new WeakSet();
var C = (function (_super) {
    __extends(C, _super);
    function C(name) {
        if (name === void 0) { name = ''; }
        var _this = _super.call(this, name) || this;
        if (cached.has(_this))
            return _this; // <- here
        cached.add(_this);
        return _this;
    }
    return C;
}(S));
console.log(new C() === new C()); // true

Actual behavior:

var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var cache = new Map();
var S = (function () {
    function S(name) {
        if (name === void 0) { name = ''; }
        return cache.has(name)
            ? cache.get(name)
            : cache.set(name, this).get(name);
    }
    return S;
}());
var cached = new WeakSet();
var C = (function (_super) {
    __extends(C, _super);
    function C(name) {
        if (name === void 0) { name = ''; }
        var _this = _super.call(this, name) || this;
        if (cached.has(_this))
            return; // <- here
        cached.add(_this);
        return _this;
    }
    return C;
}(S));
console.log(new C() === new C()); // false

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: TransformsRelates to the public transform APIFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions