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

TypeError: b is undefined in __extends #4341

Closed
drake7707 opened this issue Aug 17, 2015 · 9 comments
Closed

TypeError: b is undefined in __extends #4341

drake7707 opened this issue Aug 17, 2015 · 9 comments
Assignees
Labels
Bug A bug in TypeScript Duplicate An existing issue was already created

Comments

@drake7707
Copy link


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

@mhegazy mhegazy added the Bug A bug in TypeScript label Aug 17, 2015
@mhegazy mhegazy added this to the TypeScript 1.6 milestone Aug 17, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Aug 17, 2015

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.

@nbellowe
Copy link

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;
})()

@DanielRosenwasser
Copy link
Member

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

@danquirk
Copy link
Member

As mentioned, dupe of #2854

@danquirk danquirk added the Duplicate An existing issue was already created label Aug 20, 2015
@mhegazy mhegazy reopened this Aug 20, 2015
@mhegazy mhegazy removed the Duplicate An existing issue was already created label Aug 20, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Aug 20, 2015

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

@TylerBrinkley
Copy link

I'd love to see this fix soon. It's a huge pain when using a code generator.

@rodrigogq
Copy link

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.

@RyanCavanaugh
Copy link
Member

@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.

@mhegazy
Copy link
Contributor

mhegazy commented Feb 22, 2016

closing in favor of #5207

@mhegazy mhegazy closed this as completed Feb 22, 2016
@mhegazy mhegazy added the Duplicate An existing issue was already created label Feb 22, 2016
Layoric added a commit to ServiceStack/ServiceStack that referenced this issue Jul 14, 2016
- Order of base types causing issues if base class is declared after.
microsoft/TypeScript#4341
- `createResponse` generating invalid code for primitives
mythz pushed a commit to ServiceStack/ServiceStack that referenced this issue Jul 14, 2016
* Fixing issues with TS generation

- Order of base types causing issues if base class is declared after.
microsoft/TypeScript#4341
- `createResponse` generating invalid code for primitives

* Add back `ToHashSet()`.
@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 Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

9 participants