I need to assign some object to class property and call this object property from this assigned class.
class Animal<Creature> {
public creature: Creature;
constructor(creature: Creature) {
this.creature = creature;
}
makeSound() {
}
eat(){
// how to make this works in typescript
this.creature.howItEat()
}
}
class urchin {
data: any;
constructor(data: any) {
this.data = data;
}
public scieneName: string = "Echinoidea"
howItEat() {}
}
class seaAnimal<animal> extends Animal<animal> {
animal: animal;
constructor(animal: animal){
super(animal)
this.animal = animal
// try assign animal to this
// Object.assign(this, animal)
// how to make this works in typescript
this.animal.howItEat();
console.log(this.animal.scieneName);
}
}
let firstSeaAnimal = new urchin({name: 'urchin1', size: '15cm', weight: '200g'});
let animal1 = new seaAnimal<urchin>(firstSeaAnimal);
I'm new to typescript. I tried to implement the generic type but failed. I already tried Object.assign() but still can't get rid the error. Got from this question Typescript dynamically assign class properties