【问题标题】:How to annotate @attributes in google-closure interfaces如何在 google-closure 接口中注释 @attributes
【发布时间】:2016-06-13 23:37:11
【问题描述】:

如何在 google 闭包中注释 JSON 响应的接口?

我必须使用 JSONP 接口,它实际上将一些 XML 转换为 JSON,并将其作为参数提供给我的回调。但不幸的是,原始 XML 包含一些属性,所以我得到一个带有 @attributes-fields 的 JSON-Object,例如

{
  "output": {
    "foo": "bar"
  },
  "@attributes": {
    "baz": "attr"
  }
}

我创建了一个 google-closure 接口,因此编译器不会意外缩小我的字段,并且我在我的 IDE 中获得了简洁的自动编译。

/**
 * @interface
 */
var JsonResult = function() {};

/**
 * @type {JsonResultOutput}
 */
JsonResult.prototype.output;

/**
 * @type {JsonResultAttributes}
 */
JsonResult.prototype['@attributes'];

/**
 * @interface
 */
var JsonResultOutput = function() {};

/**
 * @type {string}
 */
JsonResultOutput.prototype.foo;

/**
 * @interface
 */
var JsonResultAttributes = function() {};

/**
 * @type {string}
 */
JsonResultAttributes.prototype.baz;

不幸的是,如果我尝试在括号和字符串中注释字段,编译器会发出警告。所以我现在的问题是:我应该如何注释它以删除警告?也许我可以在界面中删除这个字段,因为我必须在代码中以相同的方式编写这个字段,所以编译器永远不会缩小这个字段。但我也想以完整的结构记录对象。

【问题讨论】:

    标签: javascript json google-closure-compiler jsdoc


    【解决方案1】:

    这对我来说似乎是一个错误。随时通过https://github.com/google/closure-compiler提出问题

    作为一种解决方法,您必须使用对象文字键:

    JsonResult.prototype = {
      /**
       * @type {JsonResultAttributes}
       */
      '@attributes': {} 
    };
    

    【讨论】:

    • 感谢您的好主意。我通过在加载后立即将结果转换为更面向目标的格式并仅注释其他字段来解决方法。我还在转换和界面中添加了描述性注释以记录问题,但您的解决方案看起来要好得多。不过,我会为这种行为写一张票,以阐明如何正确处理这种情况。
    猜你喜欢
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    • 2021-01-22
    • 2010-09-13
    • 1970-01-01
    相关资源
    最近更新 更多