JSDoc Object of templated objects

2019-06-28 03:45发布

Is there a way to loosely specify what type of objects should be inside the object you're documenting?

I'm looking to document an object of the following:

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"
  }
};

The sub objects should have the following properties:

/**
 * 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 
 */

If I wanted to document this specific obj I would do the following, but the object will be dynamically generated with an unknown number of objects of unknown name with the type of 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
 */

P.S. I'm just looking for a wildcard @property that can be used so my editor can help me remember all the options available for each sub-object inside the object.

1条回答
走好不送
2楼-- · 2019-06-28 04:05

Per http://usejsdoc.org/tags-type.html , as of JSDoc 3.2, JSDoc has had full support of Google Closure Compiler type expressions. One such format is described at http://usejsdoc.org/tags-type.html#jsdoc-types :

{Object.<string, number>}

So in your case, you should be able to do:

/**
 * Object of Special Objects
 * @typedef mySpecialObjectOfObjects
 * @type {Object.<string, com.namespace.MyClass.myObject>}
 * @memberof com.namespace.MyClass
 */

You could even replace string there with its own type, if you wanted to have a special type dedicate to the name detailing the allowable string values.

查看更多
登录 后发表回答