Search code examples
javascriptnode.jswebstormjsdoc

WebStorm JSDoc class declaration for code completion with node.js modules


I'd like to know how to correctly use JSDoc in WebStorm 11 to mark classes and types in a node.js application to get code completion etc.

Example:

file A:

function A(){
    this.b = true;
}
A.prototype.doSth = function doSth() {
    return !this.b;
}
module.export = new A(); 

file B:

var foo = bar(); // returns type of A

Now I'd like to have code completion for "foo.", so that it suggests foo.b, foo.doSth, ...

I tried a lot of JSDoc entries like @class, @constructor and used @type {A} to define type of foo like

/* @type {A} */
var foo = bar();

but WebStorm does not recognize the type A. it does no code completion and also Ctrl+Click on the type A in brackets says "Cannot find declaration to go to".

Any idea how to do this correctly?


Solution

  • Seems like I found a solution. The problem was just the Syntax I used to mark the variable type:

    /**
     * @class
     */
    function A(){
        this.b = true;
    }
    A.prototype.doSth = function doSth() {
        return !this.b;
    }
    module.export = new A(); 
    

    Works with

    /**
     * @type {A}
     */
    var foo = bar();
    

    Notice the difference in the comment syntax: Previously I tried to use

    /* @type {A} */
    var foo = bar();
    

    Which does not work. @class can also be exchanged with @constructor