Answers for "class inheritance in typescript"

1

TypeScript Class Inheritance Example

class Animal {  private name: string;  constructor(theName: string) {    this.name = theName;  }}
class Rhino extends Animal {  constructor() {    super("Rhino");  }}
class Employee {  private name: string;  constructor(theName: string) {    this.name = theName;  }}
let animal = new Animal("Goat");let rhino = new Rhino();let employee = new Employee("Bob");
animal = rhino;animal = employee;Type 'Employee' is not assignable to type 'Animal'.
  Types have separate declarations of a private property 'name'.2322Type 'Employee' is not assignable to type 'Animal'.
  Types have separate declarations of a private property 'name'.Try
Posted by: Guest on July-14-2021
5

classes in typescript

class Info {
  private name: string ;
  constructor(n:string){
    this.name = n ;
  };
  describe(){
    console.log(`Your name is  ${this.name}`);
  }
}

const a = new Info('joyous');
a.describe();
Posted by: Guest on October-17-2020
0

TypeScript Class Inheritance

class Animal {
  move(distanceInMeters: number = 0) {
    console.log(`Animal moved ${distanceInMeters}m.`);
  }
}

class Dog extends Animal {
  bark() {
    console.log("Woof! Woof!");
  }
}

const dog = new Dog();
dog.bark();
dog.move(10);
dog.bark();
Posted by: Guest on July-14-2021
0

class inheritance in typescript

// Parent class
class Info {
  protected name: string ;
  constructor(n:string){
    this.name = n ;
  };
  describe(){
    console.log(`Your name is  ${this.name}`);
  }
}

//inherited class (you can overwrite methods of parent class, super is used to
// connect to the parent parameter(s) . )

class Detail extends Info{
  constructor(name:string, public age:number){
    super(name);
  }
  findAge(){
      console.log(`${this.name} age is ${this.age}`)
  }
}

const b = new Detail('jank', 23);
b.describe();
b.findAge();
Posted by: Guest on October-17-2020

Code answers related to "class inheritance in typescript"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language