如何在Webstorm中对抗大量未解决的变量警告

Ser*_*lov 105 javascript coding-style webstorm

好的,我有一个从ajax获取数据的函数:

function getData(data){
    console.log(data.someVar);
}
Run Code Online (Sandbox Code Playgroud)

Webstorm说someVar- 是未解决的变量.我不知道如何处理这么多警告.

我看到几个选项:

  • 压制警告;
  • 添加带有字段的json源文件(更多详细信息);
  • 使用类似数组的语法:( data['some_unres_var']/** @namespace data.some_unres_var*/警告我不要这样做);
  • ???

Webstorm也让我为"数据"创建命名空间(添加注释someVar),创建这样的字段或重命名它.

And*_*din 95

使用JSDoc:

/**
 * @param {{some_unres_var:string}} data
 */
function getData(data){
    console.log(data.some_unres_var);
}
Run Code Online (Sandbox Code Playgroud)

  • 对于变量,使用此语法`/***@type {Object}*@property {string} sortval - value按*/var a排序; (7认同)
  • 当函数是anonymouns函数时,你会怎么做?如........ .then(function(data){....}) (3认同)
  • 有没有类似的方法来定义全局变量?我在我的网络应用程序中引用了一个外部库,我需要使用诸如“MediumEditor”之类的东西,但是 intellij 给了我臭名昭著的未解决的变量警告。 (2认同)

Bob*_*ein 43

JSDoc对象.然后是其成员.

/**
 * @param data          Information about the object.
 * @param data.member   Information about the object's members.
 */
function getData(data){
    console.log(data.member);
}
Run Code Online (Sandbox Code Playgroud)
  • @property 用于局部变量(非参数)
  • 在PyCharm中测试过.@Nicholi证实它适用于Webstorm.
  • {{ member:type }}Andreas建议的语法可能与Django模板冲突.
  • 感谢Jonny Buchanan的回答引用了@param wiki.

要记录对象数组,请使用[]括号作为JSDoc 建议:

/**
 * @param data
 * @param data.array_member[].foo
 */
Run Code Online (Sandbox Code Playgroud)


Dan*_*scu 15

所有其他答案对于一般情况都是不正确的.如果你没有data作为参数怎么办?那你没有JSDoc:

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;
}
Run Code Online (Sandbox Code Playgroud)

WebStorm将警告"result.entries"是一个未解析的变量(字段).

一般的解决方案是添加一个@namespace声明:

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;
}
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的解决方案。我有很多从API返回的属性,因此我想使用您的技术来列出其中的许多属性,以避免出现这样的错误:```/ ** @namespace req.headers.signaturecertchainurl ** / / ** @namespace req.headers.signature ** / / ** @namespace req.headers.slots ** / / ** @namespace req.headers。Nutritionslot ** /```是否有创建更高版本的方法级别名称空间(例如`req.headers`)并自动为其分配子级?(很抱歉,注释中没有格式!) (2认同)

小智 7

解构使用,卢克。

function getData(data){
    const {member} = data;
    console.log(member);
}
Run Code Online (Sandbox Code Playgroud)


len*_*ena 6

使用带有匿名函数表达式的伪js文件返回json文字,如http://devnet.jetbrains.com/message/5366907所示,可能是一个解决方案.我还建议创建一个保存此json值的伪变量,并将此var用作@param注释的值,以使WebStorm知道实际类型是什么.喜欢:

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

另见http://devnet.jetbrains.com/message/5504337#5504337

  • Elena 在 JetBrains 论坛上的建议是一个[奇怪的解决方案](https://intellij-support.jetbrains.com/hc/en-us/community/posts/206349469/comments/115000258810)。一般的解决方案是[使用@namespace](http://stackoverflow.com/questions/20835544/how-to-fight-a-lots-of-unresolved-variables-warning-in-webstorm/44093516#44093516)。 (2认同)