extends and implements in one class in TypeScript

Everton Santos picture Everton Santos · Mar 30, 2015 · Viewed 25.9k times · Source

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

Answer

basarat picture basarat · Mar 30, 2015

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.