JSDoc: Return object structure

后端 未结 3 1947
独厮守ぢ
独厮守ぢ 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条回答
  •  旧时难觅i
    2020-12-22 17:35

    An alternative to the suggestions already posted, you can use this format:

    /**
     * Get the connection state.
     *
     * @returns {Object} connection The connection state.
     * @returns {boolean} connection.isConnected Whether the authenticated user is currently connected.
     * @returns {boolean} connection.isPending Whether the authenticated user's connection is currently pending.
     * @returns {Object} connection.error The error object if an error occurred.
     * @returns {string} connection.error.message The error message.
     * @returns {string} connection.error.stack The stack trace of the error.
     */
    getConnection () {
      return {
        isConnected: true,
        isPending: false,
        error
      }
    }
    

    which will give the following documentation output:

        Get the connection state.
    
        getConnection(): Object
    
        Returns
        Object: connection The connection state.
        boolean: connection.isConnected Whether the authenticated user is currently connected.
        boolean: connection.isPending Whether the authenticated users connection is currently pending.
        Object: connection.error The error object if an error occurred.
        string: connection.error.message The error message.
        string: connection.error.stack The stack trace of the error.
    

提交回复
热议问题