Skip to content

TypeError: b is undefined in __extends #4341

Closed

Description

@drakarah

class B extends A {
    constructor(msg:string) { 
        super(msg);
    }
}

class A {
    constructor(public msg:string) {

    }
}

If the super class of B is defined after A you'll get a runtime error.

Just tested the above code in the typescript playground and I get the same error

Activity

  1. mhegazy commented on Aug 17, 2015

    @mhegazy
    Contributor

    This issue has been discussed in #21 and has been closed for some bigger issue. this has been a reoccurring pain though.

    The consensuses in #21 is to show a compiler error about what the wrong order. we should be able to do this easily using checker.ts::isDefinedBefore.

  2. nbellowe commented on Aug 20, 2015

    @nbellowe

    Why isn't this expected behavior?

    class B extends A {
        constructor(msg:string) { 
            super(msg);
        }
    }
    class A {
        constructor(public msg:string) {
    
        }
    }
    

    Generates

    var __extends = ... we've all seen this! :) 
    var B = (function (_super) {
        __extends(B, _super);
        function B(msg) {
            _super.call(this, msg);
        }
        return B;
    })(A);
    var A = (function () {
        function A(msg) {
            this.msg = msg;
        }
        return A;
    })();
    

    The IIFE after var B = has a parameter of A, but var A = ...isn't called until just after that.

    I don't know how you could get the above functionality ... maybe:

    var B;
    var A;
    (function(){
        function b(_super) {
            __extends(B, _super);
            function B(msg) {
                _super.call(this, msg);
            }
            return B;
        }
        function a(){ ... }
        B = b(a);
        A = a;
    })()
    
  3. DanielRosenwasser commented on Aug 20, 2015

    @DanielRosenwasser
    Member

    It's expected, it's just that the compiler should warn you that this will happen.

  4. danquirk commented on Aug 20, 2015

    @danquirk
    Member

    As mentioned, dupe of #2854

  5. mhegazy commented on Aug 20, 2015

    @mhegazy
    Contributor

    Sheetal Nandi (@sheetalkamat) has a fix for the class issue since it has been biting users quite frequently.

  6. 3 remaining items

  7. rodrigogq commented on Dec 14, 2015

    @rodrigogq

    I am having the same issue even if the class is used after the other:

    BaseClass.ts:

    module MyModule {
      class Base {
        constructor() { }
      }
    }
    

    ChildClass.ts:

    module MyModule {
      class Child extends Base {
        constructor() { super(); }
      }
    }
    

    Generates:

    /****************************************************************************
      Generated by T4TS.tt - don't make any changes in this file
    ****************************************************************************/
    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 MyModule;
    (function (MyModule) {
      /** Generated from MyModule.Base **/
      var Base = (function () {
        function Base () {
        }
        return Base;
      })();
      MyModule.Base = Base;
    })(MyModule || (MyModule = {}));
    var MyModule;
    (function (MyModule) {
      /** Generated from MyModule.Child **/
      var Child = (function (_super) {
          __extends(Child, _super);
          function Child() {
              _super.apply(this, arguments);
          }
          return Child;
      })(MyModule.Base);
      MyModule.Child = Child;
    })(MyModule|| (MyModule = {}));
    
    

    It seems that the order is correct, although it doesn't look like it is working as expected.

  8. RyanCavanaugh commented on Dec 14, 2015

    @RyanCavanaugh
    Member

    Rodrigo G. de Queiroz (@rodrigogq) the generated code you posted runs without error. The input code you posted has a compile error because Base is not exported from its module and is thus invisible in the second declaration.

  9. mhegazy commented on Feb 22, 2016

    @mhegazy
    Contributor

    closing in favor of #5207

  10. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptDuplicateAn existing issue was already created

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions