Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error with super structure in inheritance attempt (typescript classes)


class Character {
    public readonly name: string;
    public readonly level: number;

    constructor(name: string, level: number) {
        this.name = name;
        this.level = level;
    }

    walk() {
        return `${this.name} walking!`
    }

    showMyLevel() {
        return `${this.name} has the level ${this.level}`
    }
}

class Wizzard extends Character {
    private readonly cajado: string;

    constructor(cajado: string) {
        super(name, level);
        this.cajado = cajado;
    }

    fireBall() {
        return `${this.name} fire ball!`
    }

}

const wizzard1 = new Wizzard('Ray', 8);

console.log(wizzard1.fireBall());

I would like my Wizard class to inherit my Character class and implement a constructor with one more item. However in super() I am getting errors

const name: void
@deprecated

'name' is deprecated.ts(6385)
lib.dom.d.ts(17877, 5): The declaration was marked as deprecated here.
Argument of type 'void' is not assignable to parameter of type 'string'.ts(2345)
Cannot find name 'level'. Did you mean the instance member 'this.level'?ts(2663)

I thought this could be happening because the variables are private in readonly, but changing it to public returns the same problem. Would you help me?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to add parameters to the Wizzard class that match the Character class.

class Wizzard extends Character {
    private readonly cajado: string;

    constructor(name: string, level: number, cajado: string) {
        super(name, level);
        this.cajado = cajado;
    }

    fireBall() {
        return `${this.name} fire ball!`
    }

}

Also note that Wizard is the correct spelling, not Wizzard.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading