JSDoc: Return object structure

后端 未结 3 1952
独厮守ぢ
独厮守ぢ 2020-12-22 17:13

How can I tell JSDoc about the structure of an object that is returned. I have found the @return {{field1: type, field2: type, ...}} description syntax and trie

3条回答
  •  半阙折子戏
    2020-12-22 17:45

    Define your structure separately using a @typdef:

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

    And use it as the return type:

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

提交回复
热议问题