javascript - How to inherit method types from parent class with JsDoc? - Stack Overflow

I'd like to be able to write the JsDoc for an abstract method in the parent class, such that it ap

I'd like to be able to write the JsDoc for an abstract method in the parent class, such that it applies to all its inheritors.

This example is an extremely stripped-down version of my case:

class Shape {
    /** Does some plicated example stuff.
     * @param {number} myNumber a real value.
     * @param {boolean} myBoolean a flag.
     * @param {string} myString some text.
     * @returns {string} something important.
     */
    doStuff(myNumber, myBoolean, myString) {}
}

The doStuff method is not implemented, because each of the (many) subclasses implements it differently, but with the same signature and the same parameters.

class Triangle extends Shape {
    doStuff(myNumber, myBoolean, myString) {
        return myBoolean ? myNumber.toString() : myString;
    }
}

As you can see from the screenshot, the text parts of the documentation are inherited, but the parameter types and return type are lost. What can I do to correctly inherit the types as well? I tried playing around with the @inheritdoc, @abstract, @extends, @override and @link tags but with no success.

Please consider that this is an extremely simplified version, in my actual code the documentation block is upwards of 20 lines and copying it over to an ever-increasing number of subclasses is not feasible as it would be a nightmare to maintain.

I'd like to be able to write the JsDoc for an abstract method in the parent class, such that it applies to all its inheritors.

This example is an extremely stripped-down version of my case:

class Shape {
    /** Does some plicated example stuff.
     * @param {number} myNumber a real value.
     * @param {boolean} myBoolean a flag.
     * @param {string} myString some text.
     * @returns {string} something important.
     */
    doStuff(myNumber, myBoolean, myString) {}
}

The doStuff method is not implemented, because each of the (many) subclasses implements it differently, but with the same signature and the same parameters.

class Triangle extends Shape {
    doStuff(myNumber, myBoolean, myString) {
        return myBoolean ? myNumber.toString() : myString;
    }
}

As you can see from the screenshot, the text parts of the documentation are inherited, but the parameter types and return type are lost. What can I do to correctly inherit the types as well? I tried playing around with the @inheritdoc, @abstract, @extends, @override and @link tags but with no success.

Please consider that this is an extremely simplified version, in my actual code the documentation block is upwards of 20 lines and copying it over to an ever-increasing number of subclasses is not feasible as it would be a nightmare to maintain.

Share Improve this question edited Jan 29, 2022 at 13:37 Bergi 666k161 gold badges1k silver badges1.5k bronze badges asked Jan 29, 2022 at 13:14 DartnixDartnix 1171 silver badge7 bronze badges 2
  • 1 Don't think it's possible. There's a couple of duplicates (stronger and weaker), still open. – raina77ow Commented Jan 29, 2022 at 13:42
  • 1 Another one: stackoverflow./questions/55728587/… – raina77ow Commented Jan 29, 2022 at 13:45
Add a ment  | 

3 Answers 3

Reset to default 5

The following pattern will work (though admittedly not perfect)

class Triangle extends Shape {
    /** @type {Shape['doStuff']} */
    doStuff(myNumber, myBoolean, myString) {
        return myBoolean ? myNumber.toString() : myString;
    }
}

How about you wrap your doStuff() method inside it's own @typedef?

https://jsdoc.app/tags-typedef.html

/**
* @typedef doStuff
* @type {Function}
* @param {Number} myNumber
* @param {Boolean} myBoolean
* @param {String} myString
* @returns {String}
*/

And then use it in extends:

class Triangle extends Shape {
  /** @type {doStuff} */
  doStuff(){}
}

I don't think this is possible, as this is a shorting of JSDoc.

It's anyway arguable that parameter types should be taken from the respective base class function.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744210651a4563315.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信