Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
   }
}
like image 437
MR.ABC Avatar asked Jun 21 '13 09:06

MR.ABC


2 Answers

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()).

like image 164
Jude Fisher Avatar answered Sep 28 '22 09:09

Jude Fisher


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

like image 20
Mark Broadhurst Avatar answered Sep 28 '22 09:09

Mark Broadhurst