inheritance - TypeScript: assign implemented class to variable typed as base class -


i want create base class other classes implement and/or extend. derived classes doing because share identical functionality, have own spin on piece of it.

abstract class human {     constructor(protected _name:string) {     }      protected identify():string {         return this._name;     }      abstract dotrick():void; }  class boy extends human {     private jump():void {         console.log("jumping");     }      private dotrick():void {         this.jump();     } } 

this works fine. boy can created , he'll name based on human says when identify() happens. problem hits here:

var person:human = new boy("bob"); 

i want because have variety of human derived classes , don't want have var person:boy|girl|man|child|dinosaur|whatever. problem doing causes error ts2322: type 'boy' not assignable type 'human'.

i feel should able type person human though i'm assigning derived boy class it, since boy class implementation of human class.

what missing?


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -