简体   繁体   English

JSDoc提供功能参数属性+ WebStorm

[英]JSDoc for function parameter properties + WebStorm

What is the correct JSDdoc declaration for company being a String in the code below? 以下代码中company为String的正确JSDdoc声明是什么?

 fetch('https://api.github.com/users/mdo').then(res => res.json()).then(res => { console.log(res.company); }); 

I use WebStorm and it understandably underscores company as an Unresolved variable : 我使用WebStorm,可以理解,它强调了company作为Unresolved variable

WebStorm强调属性为未解决的变量

if I add 如果我加上

/** @namespace bogus.company **/

anywhere in the file, WebStorm is happy, but that doesn't make sense: 在文件中的任何位置,WebStorm都很高兴,但这没有任何意义:

WebStorm对虚假声明感到满意

Is this a bug in WebStorm, or am I missing something about how JSDoc declarations are supposed to work? 这是WebStorm中的错误,还是我缺少有关JSDoc声明应该如何工作的信息?

Is JSDoc even supposed to be used for this use case? JSDoc是否甚至应该用于此用例?

If you check the inspection for unresolved javascript variable there is a Report undeclared properties as error option that you could uncheck. 如果您检查检查unresolved javascript variable ,则可以取消Report undeclared properties as error选项。

I like to do something like this: 我喜欢做这样的事情:

/**
 * @param {Object} res
 * @property {String} company
 */
function myOutput(res) {
  console.log(res.company);
}

fetch('https://api.github.com/users/mdo')
  .then(myToJson)
  .then(myOutput);

But for small anonymous functions I wouldn't even bother. 但是对于小的匿名函数,我什至不会打扰。 It's a weak warning so turn the inspection off or learn to live with it, I'd say. 我会说,这是一个微弱的警告,因此请关闭检查或学习使用它。 It's possible to cram in /** @param {*} something */ in anonymous functions as well, but often it only serves to reduce readability. 也可以在匿名函数中的/** @param {*} something */ ,但通常只会降低可读性。

Another option is to make a dummy file soomething.js somewhere under your content-root with an anonymous function like: 另一个选择是在内容根目录下的某个位置使用匿名函数制作一个伪文件soomething.js ,该匿名函数如下:

(() => {
  return {
    "github-users-response" : {
      "company" : "sample"
    }
  }
});

And now webstorm will figure that company actually is defined in your other files. 现在webstorm会的数字,公司实际上在其他文件中定义。 You can even put it in a different content-root if you want to keep it separate from your project. 如果要将其与项目分开,甚至可以将其放在其他内容根目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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