MR.ABC
MR.ABC

Reputation: 4862

Typescript call function from base class

is there a way to call a function from baseclass like overwrite.

Base Class

export class BaseClass {
   constructor() {
   //do something asynchronous
   //than call initialized
   }
}

Inheritance Class

export class InheritanceClass extends BaseClass {
   initialized() {
   // get called from base class
   }
}

Upvotes: 7

Views: 7242

Answers (2)

Jude Fisher
Jude Fisher

Reputation: 11284

Do you mean like this:

class Base {
    constructor(){
        setTimeout(()=>{
            this.initialized();
        }, 1000);
    }

    initialized(){
        console.log("Base initialized");
    }
}

class Derived extends Base {
    initialized(){
        console.log("Derived initialized");
    }
}

var test:Derived = new Derived(); // console logs "Derived initialized" - as expected.

Works well in the Playground (Ignore the odd red underline on setTimeout(), which I think is a bug - it compiles and runs fine.)

You do need the method present on Base, but you can override it in Derived (with or, as in this case, without a call to super.initialized()).

Upvotes: 9

Mark Broadhurst
Mark Broadhurst

Reputation: 2695

In order to do that you would need to have initialized as an abstract member of the abstract class. typescript currently does not support this however the feature has been asked for and there is a work item open for it:

http://typescript.codeplex.com/workitem/395

Upvotes: 2

Related Questions