shal
shal

Reputation: 3024

How to define JSDoc for such an object?

For example I have an object description via @name:

/**
@name Point
@prop {number} x
@prop {number} y
*/

And an object, where each property is Point:

/**
 * 
 * @type {what?}
 */
var details = {
   something: {x:1.1, y: 2.2},
   another: {x:1.1, y: 2.2},
   theRest: {x:1.1, y: 2.2},
   more: {x:1.1, y: 2.2},
   yetAnother: {x:1.1, y: 2.2}
};

What type should it be? Is it possible to set the type only by property values, without keys? Because I'm going to add/remove properties even on the fly, but all values will always be Point.

Is it possible to describe using jsDoc?

Upvotes: 3

Views: 2291

Answers (1)

Graham P Heath
Graham P Heath

Reputation: 7389

As I understand it there are 2 approaches to defining the keys and types of an object in JSDocs.

JSDocs as defined by usejsdoc.com uses @property:

/**
  @typedef PropertiesHash
  @type {object}
  @property {string} id - an ID.
  @property {string} name - your name.
  @property {number} age - your age.
 /

/** @type {PropertiesHash} /
var props;

Where as when used at Google, specifically with Google Closure prefers a {{key:(type)}} structure:

/**
 * A typedef to represent a CSS3 transition property. Duration and delay
 * are both in seconds. Timing is CSS3 timing function string, such as
 * 'easein', 'linear'.
 *
 * Alternatively, specifying string in the form of '[property] [duration]
 * [timing] [delay]' as specified in CSS3 transition is fine too.
 *
 * @typedef { {
 *   property: string,
 *   duration: number,
 *   timing: string,
 *   delay: number
 * } | string }
 */
goog.style.transition.Css3Property;

To answer your question directly, it sounds like you don't know all of your keys, so you'll have to use a simpler definition.

/** @type {Object<string, Point>} */

or shorthand;

/** @type {Object<Point>} */

Upvotes: 5

Related Questions