How to fight tons of unresolved variables warning in WebStorm?

前端 未结 6 1815
陌清茗
陌清茗 2020-12-04 09:19

I have a function that takes data from server:

function getData(data){
    console.log(data.someVar);
}

WebStorm says that someVar

相关标签:
6条回答
  • 2020-12-04 09:55

    Use JSDoc:

    /**
     * @param {{some_unres_var:string}} data
     */
    function getData(data){
        console.log(data.some_unres_var);
    }
    
    0 讨论(0)
  • 2020-12-04 09:59

    JSDoc the object. Then its members.

    /**
     * @param data          Information about the object.
     * @param data.member   Information about the object's members.
     */
    function getData(data){
        console.log(data.member);
    }
    
    • @property for local variables (non parameters).
    • Tested in PyCharm. @Nicholi confirms it works in WebStorm.
    • The {{ member:type }} syntax Andreas suggested may conflict with Django templates.
    • Thanks to Jonny Buchanan's answer citing the @param wiki.

    To document arrays of objects, use [] brackets as JSDoc suggests:

    /**
     * @param data
     * @param data.array_member[].foo
     */
    
    0 讨论(0)
  • 2020-12-04 10:01

    using a dummy js file with anonymous function expression returning the json literal, as written at http://devnet.jetbrains.com/message/5366907, may be a solution. I can also suggest creating a fake variable that will hold this json value, and use this var as a value of @param annotation to let WebStorm know what the actual type is. Like:

    var jsontext = {"some_unres_var":"val"};
    /** @param {jsontext} data */
    function getData(data){
        console.log(data.some_unres_var);
    }
    

    See also http://devnet.jetbrains.com/message/5504337#5504337

    0 讨论(0)
  • 2020-12-04 10:12

    Destructuring use, Luke.

    function getData(data){
        const {member} = data;
        console.log(member);
    }
    
    0 讨论(0)
  • 2020-12-04 10:14

    All other answers are incorrect for the general case. What if you don't get data as a parameter? You don't have JSDoc then:

    function niceApiCall(parameters) {
      const result = await ...  // HTTP call to the API here
      for (const e of result.entries) {
        .. // decorate each entry in the result
      }
      return result;
    }
    

    WebStorm will warn that "result.entries" is an unresolved variable (field).

    The general solution is to add an @namespace declaration:

    function niceApiCall(parameters) {
      /** @namespace result.entries **/
      const result = await ...  // HTTP call to the API here
      for (const e of result.entries) {
        .. // decorate each entry in the result
      }
      return result;
    }
    
    0 讨论(0)
  • 2020-12-04 10:19

    To remove the warnings on The WebStorm IDE you can simply uncheck the inspection options for:

    • Unresolved Javascript function
    • Unresolved Javascript variable

    ps. this will remove the warnings on the IDE, but I don't think that is the best idea, because we will lost one of the best utilities in a IDE like Webstorm, which can worsen the quality of our code.

    Even so, if you want to follow in the menu: File > Settings > Editor > Inspections we can disable the Javascript warnings

    Like the following picture:

    0 讨论(0)
提交回复
热议问题