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

Fails to extend a class declared in condition if another function creates an instance of it #20104

Closed
peaBerberian opened this issue Nov 17, 2017 · 1 comment
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@peaBerberian
Copy link

peaBerberian commented Nov 17, 2017

TypeScript Version: 2.6.1

Code

class A {}
if (true) {
  class B extends A {}

  const foo = function () {
    new B();
  }
}

Expected behavior:

Should work as intended.

Actual behavior:

This translates into:

var __extends = (this && this.__extends) || (function () {
    var 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 function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var A = /** @class */ (function () {
    function A() {
    }
    return A;
}());
if (true) {
    var B_1 = /** @class */ (function (_super) {
        __extends(B_1, _super);
        function B() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        return B;
    }(A));
    var foo = function () {
        new B_1();
    };
}

This is problematic, especially the following line towards the end:

        __extends(B_1, _super);

B, the "constructor" should be extended. Not B_1, the "class" (which has a different name here for some reason, maybe that's part of the problem) as B_1 is not yet defined here.

This causes the parsing to fail.

@mhegazy mhegazy added the Bug A bug in TypeScript label Nov 17, 2017
@mhegazy mhegazy added this to the TypeScript 2.7 milestone Nov 17, 2017
@peaBerberian
Copy link
Author

For others with the same problem, I can confirm that - from the previous example - renaming every B to B_1 in the transpiled js file, as ugly and bothersome as it sounds, fixes the issue.

Thanks for considering this issue by the way !

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jan 8, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants