Skip to content
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

Incorrect class constructor #11846

Closed
falsandtru opened this issue Oct 25, 2016 · 0 comments · Fixed by #11868
Closed

Incorrect class constructor #11846

falsandtru opened this issue Oct 25, 2016 · 0 comments · Fixed by #11868
Labels
Bug A bug in TypeScript Domain: Transforms Relates to the public transform API Fixed A PR has been merged for this issue

Comments

@falsandtru
Copy link
Contributor

falsandtru commented Oct 25, 2016

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
@mhegazy mhegazy added the Bug A bug in TypeScript label Oct 25, 2016
@mhegazy mhegazy added this to the TypeScript 2.1 milestone Oct 25, 2016
@mhegazy mhegazy added the Domain: Transforms Relates to the public transform API label Oct 25, 2016
@falsandtru falsandtru changed the title Incorrect class constructor Incorrect class constructor regression Oct 25, 2016
@falsandtru falsandtru changed the title Incorrect class constructor regression Incorrect class constructor Oct 26, 2016
@mhegazy mhegazy added Fixed A PR has been merged for this issue 2.1 RC labels Oct 26, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Domain: Transforms Relates to the public transform API Fixed A PR has been merged for this issue
Projects
None yet
3 participants