如何为这样的对象定义JSDoc?

时间:2016-10-06 12:39:27

标签: javascript jsdoc jsdoc3

例如,我通过@name:

有一个对象描述
/**
@name Point
@prop {number} x
@prop {number} y
*/

一个对象,其中每个属性都是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}
};

它应该是什么类型的?是否可以仅通过属性值设置类型,而不使用键?因为我即使动态添加/删除属性,但所有值都将是Point。

是否可以使用jsDoc进行描述?

1 个答案:

答案 0 :(得分:1)

据我所知,有两种方法可以在JSDocs中定义对象的键和类型。

usejsdoc.com定义的JSDocs使用@property

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

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

在Google使用时,特别是Google Closure更喜欢{{key:(type)}}结构:

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

要直接回答您的问题,听起来您不知道所有的密钥,因此您必须使用更简单的定义。

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

或简写;

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