Search code examples
javascriptparameterswebstormdocumentation

Javascript how do I document a variable that should be an Object of a specific type


I am using WebStorm IDE. I have several classes that have static functions. For example the following will print "foo1_bar" to the console.

class Foo {
    static bar() {
        return "foo_bar";
    }
}

class Foo1 extends Foo {
    static bar() {
        return "foo1_bar";
    }
}

class Foo2 extends Foo {
    static bar() {
        return "foo2_bar";
    }
}

/**
 * @param {Object} type
 */
const test=(type)=>{
    console.log(type.bar());
}
test(Foo1);

This works and IDE says it is correct but I want to specify that the Object given to test must be of type Foo. If I put Foo inside the {} though instead of Object it fails. What is the proper way to document this?


Solution

  • Type name in @param annotation says that the parameter is an instance of corresponding type (i.e. created with new type()) so only the instance members are resolved. You can try using typeof here:

    class Foo {
        static bar() {
            return 'foo_bar'
        }
    }
    
    /**
     * @param {typeof Foo} type
     */
    const test = type => {
        console.log(type.bar()) //Unresolved function or method bar()
    }