45

Can I do this in TypeScript?

export interface IMyInterface {
  doSomething(): void;
}

export class MyBaseClass {
  myBaseClassHasProperty: string;

  constructor(){
    this.myBaseClassHasProperty = 'some value';
  }
  myBaseClassHasMethods(): void {
    console.log(this.myBaseClassHasProperty);
  }
}

export class MyClass extends MyBaseClass implements IMyInterface {
  constructor() {
    super();
  }

  doSomething(): void {
    this.myBaseClassHasMethods();
  }
}

In runtime it throws:

Uncaught ReferenceError: MyBaseClass is not defined

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
Everton Santos
  • 461
  • 1
  • 4
  • 5

1 Answers1

35

in runtime i get this Uncaught ReferenceError: MyBaseClass is not defined

Yes you can do that. The code you posted will work fine.

However I suspect in your actual code you have it split across multiple files and MyBaseClass is not executed before the code for MyClass.

Fix JavaScript ordering or use external modules to have the ordering determined by the module loader.

basarat
  • 261,912
  • 58
  • 460
  • 511
  • yes, i'm was using tsconfig.json to compile to only one app.js file and the tsc was creating a MyBaseClass.js file. But still not working, the tsc is not creating the __extends code on js file, I will gonna try more later. - tsc --version 1.4.1.0 on Mac OS X 10.8 – Everton Santos Mar 30 '15 at 02:35