Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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()
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!