Skip to content

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

@peaBerberian

Description

@peaBerberian

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.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions