3

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?

1 Answer 1

5

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>} */
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.