Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSDoc Object of templated objects

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.

like image 999
Steven10172 Avatar asked May 20 '15 21:05

Steven10172


People also ask

What is JSDoc used for?

JSDoc is a markup language used to annotate JavaScript source code files. Using comments containing JSDoc, programmers can add documentation describing the application programming interface of the code they're creating.

How to specify type in JSDoc?

The @type tag allows you to provide a type expression identifying the type of value that a symbol may contain, or the type of value returned by a function. You can also include type expressions with many other JSDoc tags, such as the @param tag.

Can you use JSDoc with TypeScript?

You can use most JSDoc type syntax and any TypeScript syntax, from the most basic like string to the most advanced, like conditional types.


1 Answers

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.

like image 55
Brett Zamir Avatar answered Sep 28 '22 06:09

Brett Zamir