Skip to content

tslib.__extends called on undefined prototype #14734

@jsayol

Description

@jsayol

TypeScript Version: 2.2.1

Code
To make things simple, here's a repo to easily reproduce this: https://github.com/jsayol/typescript-issue-14734

$ git clone https://github.com/jsayol/typescript-issue-14734.git
$ cd typescript-issue-14734
$ npm install
$ npm run build

And load index.html in the browser.

a.ts:
import { B } from './b';

export class A {
  constructor(public prop: string) {
  }

  b(): B {
    return new B(this.prop, 123);
  }

  log() {
    console.log(`Prop: ${this.prop}`);
  }
}
b.ts
import { A } from './a';

export class B extends A {
  constructor(prop: string, public otherStuff: number) {
    super(`B ${prop}`);
  }
}
index.ts
import { A } from './a';
import { B } from './b';

const a = new A('hello');
const b: B = a.b();
b.log();

Expected behavior:
Seeing "B hello" logged in the console.

For reference, putting all the code in a single file generates the correct output:

class A {
  constructor(public prop: string) {
  }

  b(): B {
    return new B(this.prop, 123);
  }

  log() {
    console.log(`Prop: ${this.prop}`);
  }
}

class B extends A {
  constructor(prop: string, public otherStuff: number) {
    super(`B ${prop}`);
  }
}

const a = new A('hello');
const b: B = a.b();
b.log();

Actual behavior:

tslib.es6.js:22 Uncaught TypeError: Object prototype may only be an Object or null: undefined
    at setPrototypeOf (<anonymous>)
    at Object.__extends [as a] (tslib.es6.js:22)
    at b.js:4
    at Object.__webpack_exports__.a (b.js:11)
    at __webpack_require__ (bootstrap 4672cf4…:19)
    at Object.defineProperty.value (bootstrap 4672cf4…:65)
    at __webpack_require__ (bootstrap 4672cf4…:19)
    at Object.<anonymous> (bundle.umd.js:108)
    at __webpack_require__ (bootstrap 4672cf4…:19)
    at Object.<anonymous> (bundle.umd.js:321)

This is the generated b.js, where the problem is:

import * as tslib_1 from "tslib";
import { A } from './a';
var B = (function (_super) {
    tslib_1.__extends(B, _super); // <--- Exception thrown here. "B" is undefined.
    function B(prop, otherStuff) {
        var _this = _super.call(this, "B " + prop) || this;
        _this.otherStuff = otherStuff;
        return _this;
    }
    return B;
}(A));
export { B };
//# sourceMappingURL=b.js.map

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions