0

I need to assign some object to class property and call this object property from this assigned class.

Typescript Playground

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

1
  • Given your example code, this approach is what I'd do, but this is very similar to the existing answer which is apparently insufficient. Could you edit the code to be a minimal reproducible example that demonstrates enough use cases to convey why this approach doesn't work? That would probably be more helpful than a verbal description of what's wrong. Commented Feb 14, 2023 at 17:06

1 Answer 1

2

You can extend Creature and animal generic to handle it

like this:

class Animal<Creature extends {howItEat:()=>any , scieneName:string}> {
...
class seaAnimal<animal extends {howItEat:()=>any,scieneName:string}> extends Animal<animal> {

or extend urchin

class Animal<Creature extends urchin> {
...
class seaAnimal<animal extends urchin> extends Animal<animal> {

Playground

but the better structure to handle it is to use different classes for each animal and no need to use animal

use can see it here (playground)

Sign up to request clarification or add additional context in comments.

6 Comments

Actually there are many difference property for animal. Like maybe tiger has property stripe color, eye color and so on. Like we include bird, mammal, insect etc.. If possible, I don't wanna declare the property and methods manually on the parent class
that's right, you must not do it, instead, you need to create a new class that extends animal class
let me edit your code a little
I updated my answer, please let me know if it's what you want
This answer looks correct for the question as asked; if the OP has a different question, they should consider editing the code in the question to include unsatisfied use cases.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.