Search code examples
javascripttypescripteclipsejsdoclanguage-server-protocol

Properties defined in prototypal base-class's ctor are not identified as own props in a derived class


I'm documenting existing code with JSDoc in Eclipse with jsconfig.json configured (could add it here, but it mainly says "es2017", and path declarations that I saw perfectly working).

I'm wondering why, when I define a parameter as having the base-class type, the IDE doesn't recognize a valid typed argument if it's a derived class.

And I saw an even weirder behaviour: although the type-checker seems to understand most of my inheritance patterns.

So almost 2 questions, but I'll describe the second one, which might solve the first one.

Say I have an abstract class declaration in JS:

/**
 * @constructor CoolDownTween
 */
const CoolDownTween = function() {
    this.currentFrame = 0;
    this.ended = false;
}

and a child-class inheriting from CoolDownTween adding a nextStep method, like this:

/**
 * @constructor CooledDownPropFadeToggleTween
 */
const CooledDownPropFadeToggleTween = function() {
    CoolDownTween.apply(this, arguments);
}
CooledDownPropFadeToggleTween.prototype = Object.create(CoolDownTween.prototype);

/**
 * @method nextStep
 * @param {Number} stepCount
 * @param {Number} timestamp
 * @return Void
 */
CooledDownPropFadeToggleTween.prototype.nextStep = function(stepCount, timestamp) {
if (timestamp >= 1000) {this.ended = true;}
this.currentFrame += stepCount;
}

Why is the JS/TS LSP in Eclipse (up-to-date) complaining about the "currentFrame property not existing on type CooledDownPropFadeToggleTween"?

Subsidiary question: I added a prop named "ended", to showcase what seems to be a bug in the type-checking: why is the IDE not showing an error when I make use of the "ended" property?

I've read the Google results that seemed related, and the questions on SO related to documenting specific inheritance patterns, and I can't find an answer on why my pretty classical approach incurs so many typing errors with documented classes.


Solution

  • Apparently you'd need to tell the type checker that CooledDownPropFadeToggleTween is a subclass of CoolDownTween, with the JSDOC @extends or @augments tag. But unfortunately the only way it seems you can do that is with actual class statements. While TypeScript supports the JSDoc @constructor tag to tell the type checker that a function will be used as a class constructor, there's currently no support for using @extends with @constructor.

    There's an open feature request at microsoft/TypeScript#38985 to change this. It is currently marked as "Awaiting More Feedback", which means that before the TS team would really consider implemented it, they would like to hear more from the community about why the feature is needed and why current workarounds do not suffice. It doesn't have much engagement at all, so I wouldn't expect to see it implemented in the near future. Still, if it matters a lot to you, it wouldn't hurt for you to give it a 👍 and supply your feedback. (It probably wouldn't help much, but if enough people do it, who knows?)