模板对象的JSDoc对象

Ste*_*172 5 javascript documentation jsdoc

有没有一种方法可以宽松地指定要记录的对象内应该包含哪种类型的对象?

我想记录以下对象:

var obj = {
  unknownName1: {
    name: "KnownValue"
  },
  unknownName2: {
    name: "KnownValue",
    offset: {
      x: 0,
      y: 0
    }
  },
  unknownName3: {
    name: "KnownValue",
    offset: {
      x: 0,
      y: 0
    },
    visible: true
  },
  unknownName4: {
    name: "KnownValue"
  }
};
Run Code Online (Sandbox Code Playgroud)

子对象应具有以下属性:

/**
 * Example Object
 * @typedef myObject
 * @type {Object}
 * @property {String} name - Name of the templated object
 * @property {Number} [offset.x] - Offset X
 * @property {Number} [offset.y] - Offset Y
 * @property {Boolean} [visible] - Is Visible
 * @memberof com.namespace.MyClass 
 */
Run Code Online (Sandbox Code Playgroud)

如果要记录此特定obj内容,则可以执行以下操作,但是该对象将动态生成,其未知数量的未知名称的对象的类型为com.namespace.MyClass

/**
 * Object of Special Objects
 * @typedef mySpecialObjectOfObjects
 * @type {Object}
 * @property {com.namespace.MyClass.myObject} unknownName1
 * @property {com.namespace.MyClass.myObject} unknownName2
 * @property {com.namespace.MyClass.myObject} unknownName3
 * @property {com.namespace.MyClass.myObject} unknownName4
 * @memberof com.namespace.MyClass
 */
Run Code Online (Sandbox Code Playgroud)

PS:我只是在寻找@property可以使用的通配符,因此我的编辑器可以帮助我记住对象内每个子对象可用的所有选项。

Bre*_*mir 5

根据http://usejsdoc.org/tags-type.html,从 JSDoc 3.2 开始,JSDoc 已经完全支持 Google Closure Compiler 类型表达式。http://usejsdoc.org/tags-type.html#jsdoc-types描述了一种这样的格式:

{Object.<string, number>}
Run Code Online (Sandbox Code Playgroud)

所以在你的情况下,你应该能够做到:

/**
 * Object of Special Objects
 * @typedef mySpecialObjectOfObjects
 * @type {Object.<string, com.namespace.MyClass.myObject>}
 * @memberof com.namespace.MyClass
 */
Run Code Online (Sandbox Code Playgroud)

您甚至可以string用它自己的类型替换那里,如果您想要一个特殊类型专用于详细说明允许的字符串值的名称。