JsDoc, ES6 and @param {Constructor}

I am trying to use JsDoc to document es6 classes. I can’t believe that you cannot pass a class as a parameter (class type, not instance type).

I tried everything, but I can not get this simple code to work so that JsDoc does not give me any warnings.

I cannot get it to work unless I create @typedef for each of my classes and then manually add all of my own and inherited ones to it. I can’t even make a mix!

Has anyone succeeded in passing a constructor / class parameter? So, is JsDoc in a static context, not an instance context?

/**
 * @class A
 */
class A {

    /**
     * @static
     */
    static helloFromClassA(){
    }
}

/**
 * @class B
 * @extends A
 */
class B extends A{

    /**
     * @static
     */
    static helloFromClassB(){
    }
}

/**
 * Class as object
 * @param {A} ClassArgument
 */
function fn1(ClassArgument){
    ClassArgument.helloFromClassA(); // Unresolved function or method helloFromClassA
    // Does not work because ClassArgument is interpreted as an
    // instance of A, not A constructor
}

/**
 * // Class as function
 * @param {Function} ClassArgument
 */
function fn2(ClassArgument){
    ClassArgument.helloFromClassA(); // Unresolved function or method helloFromClassA
    // Does not work because ClassArgument is interpreted as an
    // empty function, not A constructor
}

/**
 * // Type definition
 * @typedef {Object} AClass
 * @property {Function} helloFromClassA
 * @property {Function} super
 */

/**
 * // Trying to mixin the AClass
 * @typedef {Object} BClass
 * @property {Function} helloFromClassB
 * @mixes {AClass}
 * @mixes {A}
 */

/**
 * // Adding manually all members
 * @typedef {Object} BClass2
 * @property {Function} helloFromClassB
 * @property {Function} helloFromClassA
 */

/**
 * @param {BClass} ClassArgument
 */
function fn3(ClassArgument){
    ClassArgument.helloFromClassA(); // Unresolved function or method helloFromClassA
    // Does not work because the BClass typedef does not take
    // into account the mixin from AClass, nor from A
    ClassArgument.helloFromClassB(); // No warming
}

/**
 * @param {BClass2} ClassArgument
 */
function fn4(ClassArgument){
    ClassArgument.helloFromClassA(); // No Warning
    ClassArgument.helloFromClassB(); // No warming
    // Works because we manually defined the typedef with all own
    // and inherited properties. It a drag.
}


fn1(B);

fn2(B);

fn3(B);

fn4(B);

JsDoc problem: https://github.com/jsdoc3/jsdoc/issues/1088

+4
3

Visual Studio

@param {function(new:MyClass, SomeArgType, SecondArgType, etc...)}

, spec , .

+2

WebStorm . jsdoc, , ( ), JetBrains - @param {typeof Constructor} ( typeof typescript) @param {Constructor.}, . , WebStorm - https://youtrack.jetbrains.com/issue/WEB-17325

+1

typescript google {typeof SomeClass} ( ). , jsdoc (https://github.com/jsdoc3/jsdoc/issues/1349).

Since I want me to test my JavaScript using typescript, and also want to generate documentation, I made this jsdoc plugin to convert type expressions {typeof SomeClass}to {Class<SomeClass>}, so I can have both things: https://github.com/cancerberoSgx/jsdoc-typeof -plugin

0
source

Source: https://habr.com/ru/post/1611801/


All Articles