JSDoc: オブジェクト構造を返す 質問する

JSDoc: オブジェクト構造を返す 質問する

返されるオブジェクトの構造を JSDoc に伝えるにはどうすればよいですか。@return {{field1: type, field2: type, ...}} description構文を見つけて試してみました:

/**
 * Returns a coordinate from a given mouse or touch event
 * @param  {TouchEvent|MouseEvent|jQuery.Event} e    
 *         A valid mouse or touch event or a jQuery event wrapping such an
 *         event. 
 * @param  {string} [type="page"]
 *         A string representing the type of location that should be
 *         returned. Can be either "page", "client" or "screen".
 * @return {{x: Number, y: Number}} 
 *         The location of the event
 */
var getEventLocation = function(e, type) {
    ...

    return {x: xLocation, y: yLocation};
}

これは正常に解析されますが、結果のドキュメントには次のように記載されるだけです。

Returns: 
    The location of an event
    Type: Object

私は API を開発しており、返されるオブジェクトについて人々に知ってもらう必要があります。これは JSDoc で可能ですか? 私は JSDoc3.3.0-beta1 を使用しています。

ベストアンサー1

:を使用して@typedef構造を個別に定義します。

/**
 * @typedef {Object} Point
 * @property {number} x - The X Coordinate
 * @property {number} y - The Y Coordinate
 */

これを戻り値の型として使用します。

/**
 * Returns a coordinate from a given mouse or touch event
 * @param  {TouchEvent|MouseEvent|jQuery.Event} e    
 *         A valid mouse or touch event or a jQuery event wrapping such an
 *         event. 
 * @param  {string} [type="page"]
 *         A string representing the type of location that should be
 *         returned. Can be either "page", "client" or "screen".
 * @return {Point} 
 *         The location of the event
 */
var getEventLocation = function(e, type) {
    ...

    return {x: xLocation, y: yLocation};
}

おすすめ記事