inheritance - Accessing child class property through parent class in typescript -


i have base class called user has 2 subclasses generaluser , adminuser.

export abstract class user { private username:string; private password:string;  constructor(username:string,password:string) {   this.username=username;   this.password=password; }  username() {   return this.username; }  password() {   return this.password; } }  export class generaluser extends user {   constructor(username:string,password:string) {   super(username,password);   } }  export class adminuser extends user {    accessrights:string[];    constructor(username:string,password:string,accessrights:string[])   {   super(username,password);   this.accessrights=accessrights;   } } 

there login function takes instance of either generaluser , adminuser. hence have kept parameter of login function of type user. now, if want access property accessrights, wouldn't possible since base class user doesn't have property.

login(user:user) { if(user instanceof adminuser) { console.log(user.accessrights); //cannot access property accessrights } }  let admuser1=new adminuser("admin_xyz","1234",["delete","view"]); login(admuser1); 

should rethink logic or there workaround this?


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -