【问题标题】:How to define JSDoc for such an object?如何为这样的对象定义 JSDoc?
【发布时间】:2017-02-15 05:09:40
【问题描述】:

例如我有一个通过@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来描述吗?

【问题讨论】:

    标签: javascript jsdoc jsdoc3


    【解决方案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>} */
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-04
      • 2019-04-06
      相关资源
      最近更新 更多