简体   繁体   中英

IDE doesn't recognize method using JSDoc

I've got a function defined like the one below.

/**
 * @type Object
 * @return {typeof hello} hello
 */

function hello() {

    /**
     * Prints some words
     * @param {string} words - words to print
     * @returns {string} words you said
     */
    function sayIt(words) {
        console.log(words);
        return 'You said: ' + words;
    }
    return {
        sayIt: sayIt
    }
}

I would like it so that when I type hello. my IDE would tell me that the method sayIt is available and that it takes in the parameter words as a string.

This function is getting loaded as a module into a cloud system and the only way you can call it from another script is by importing the hello module and using it like hello.sayIt('hello') . So basically I'm wondering if there is a way to format the JSDoc so that my IDE knows that the sayIt method is available to the hello object and that it takes in a words parameter as a string. At present it knows that sayIt is a method but does not know that it is associated with the hello object, so I don't get any auto completion help.

Webstorm should be able to give you autocorrect even if you don't have JSDOC for your code.

Check that you have module.exports = hello and not module.exports = hello()

If this suggestion does not work or does not apply, I would advise refactoring your code

I would recommend you do this instead.

return {
  sayIt: function(words) {
    console.log(words);
    return 'You said: ' + words;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM