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
In TypeScript, interfaces can also extend classes, but only in a way that involves inheritance. When an interface extends a class, the interface includes all class members (public and private), but without the class' implementations.
Note: A class can extend a class and can implement any number of interfaces simultaneously.
Today, a friend ask about the difference between extends and implements . The short answer for him was: extends: The class get all these methods and properties from the parent, so you don't have to implement. implements: The class has to implement methods and properties.
Typescript allows an interface to inherit from multiple interfaces. Use the extends keyword to implement inheritance among interfaces.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With