Tom Nijs
Tom Nijs

Reputation: 3962

JSDoc override return type of inherited class method

I'm trying to provide a dynamic return type for an inherited method by passing the child Class to the parent. Please find a small example below. Currently it's not working and I'm thinking I am not far off.

I'm aware that a workaround is to manually declare each inherited method in each child class is an option, and simply calling super() from within but that's not as elegant a solution as I was hoping for.

I'm hoping someone can help me figure out the generic types or if it's at all possible.

Thanks in advance:

/**
 * The Animal base class
 * @class
 */
class Animal {

    /**
     * @param  {T} classType
     */
    constructor (classType) {
        this.classType = classType
    }
    /**
     * @returns {T} A baby of the same type as the parent instance
     */
    giveBirth () {
        return new this.classType()
    }
}

/**
 * The Dog class
 * @class
 * @extends Animal
 */
class Dog extends Animal {
    constructor () {
        super(Dog)
    }

    bark () {
        return 'woof'
    }
}

const dog = new Dog()
const babyDog = dog.giveBirth()
babyDog.bark() // JSDoc does not consider it an instance of Dog

Upvotes: 5

Views: 1188

Answers (1)

Sergey
Sergey

Reputation: 336

Adding @template for Animal and correct extend (@extends Animal<Dog>) works for me

/**
 * The Animal base class
 * @class
 * @template T
 */
class Animal {

    /**
     * @param {T} classType
     */
    constructor (classType) {
        this.classType = classType
    }
    /**
     * @returns {T} A baby of the same type as the parent instance
     */
    giveBirth () {
        return new this.classType()
    }
}

/**
 * The Dog class
 * @class
 * @extends Animal<Dog>
 */
class Dog extends Animal {
    constructor () {
        super(Dog)
    }

    bark () {
        return 'woof'
    }
}

const dog = new Dog()
const babyDog = dog.giveBirth()
babyDog.bark()

Upvotes: 4

Related Questions