Attribute on options parameter with JSDoc in WebStorm: “Unresolved variable”

穿精又带淫゛_ 提交于 2019-12-07 07:07:05

问题


I have a function with jsdoc comment that resembles the following in my code:

/**
 * Foo function
 * @param {Object} [options]
 * @param {String} [options.foo]
 */
var foo = function (options) {
    if (!options) options = {};
    var foo = options.foo || 'foo';
    // ...
};

My IDE, WebStorm, marks options.foo as an 'unresolved variable'. If I remove the jsdoc comment, the warning disappears. How do I document this function in a way that WebStorm gets the hint and no longer displays this warning?

Things that I tried:

  • Remove @param {String} [options.foo]
  • @param {?String} [options.foo]
  • @param {String} options.foo
  • if (!options) options = {foo: 'bar'};
  • The last two combined.

Is this a bug or am I missing something? WebStorm does seem to understand the options.foo param definition, because if (!options) options = {foo: 42}; raises a warning that the assigned type is not a String.

I'm using WebStorm 7.0.


回答1:


Webstorm's documentation links to jsdoc 3. With jsdoc 3, I would document your object like this:

@param {{foo: string}} options

The way you were trying to do it implies a function signature like function (options, options.foo).

Information about how to document types is available here.



来源:https://stackoverflow.com/questions/20235179/attribute-on-options-parameter-with-jsdoc-in-webstorm-unresolved-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!