Changing inherited functions typescript

Issue

Is there any way to override the contents of the asString function in my RunTimeError class as I need the error message to be different than the one in my generic Error class

class Error {
    errorName: string;
    details: string;
    posStart: Position;
    posEnd: Position;

    constructor({errorName, details, posStart, posEnd}: ErrorTypes) {
        this.errorName = errorName
        this.details = details
        this.posStart = posStart
        this.posEnd = posEnd
    }

    asString() {
        let result: string = `${this.errorName} on line ${this.posStart.ln+1}: ${this.details}`
        return result
    }
}
class RunTimeError extends Error {
    context: any

    constructor({details, posStart, posEnd, context}: SubErrorTypes) {
        super({errorName:"Runtime Error", details:details, posStart:posStart, posEnd:posEnd})
        this.context = context
    }

    //change contents of asString
}

Solution

pass a prop into the generic class function asString

Answered By – aflo7

Answer Checked By – Jay B. (AngularFixing Admin)

Leave a Reply

Your email address will not be published.