typescriptextendsimplements

extends and implements in one class in TypeScript


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


Solution

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