Home > Net >  Changing inherited functions typescript
Changing inherited functions typescript

Time:06-10

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
}

CodePudding user response:

pass a prop into the generic class function asString

  • Related