jsdoc / jsdoc/jsdoc

[Feature request] Using @returns as type reference

Open
#1,900 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
15.5k
Forks
1.5k
Avg merge
10d 23h
Merged PRs (30d)
1

Description

This may be out of scope of what JSDoc is intended to do, or is capable of doing.

Context & Problem

Some of my functions will return an object literal for the sake of convivence or because there are just so few call-sites.

Typically, I will write a @typedef to describe the return structure. However, more an more, I'm preferring to use implicitly defined types.

It would be nice (makes my life easier) if a function return could be used as the Type reference of a @typedef.

Example
Code Example. Please excuse code quality.
/** @typedef {Coffee@returns} CaffeineDrink */

/**
 * @param {Boolean} cream
 * @param {Boolean} sugar
 * @returns {Drink}
 */
export default function Coffee(cream, sugar) {
  const self = {
    privateProperties: {
      coffeeMachine: () => {},
      makeCoffee: () => Math.random(),
    },
  };

  /** @typedef {drink} Drink */
  /**
   * @member
   */
  const drink = {
    isCold: self.privateProperties.makeCoffee() > 0.5,
    hasCream: cream,
    hasSugar: sugar,
  };

  self.drink = drink;

  return self.drink;
}
Benefits

In the example above, CaffeineDrink would be a type represented as an Object {isCold: boolean, hasCream: boolean, hasSugar: boolean}.

Modifying the return of Coffee, or more specifically the drink variable, would implicitly update the structure of CaffeineDrink.

Workaround

I am using VSCode, so this could possibly be a TypeScript/VSCode thing. But it works for me, and I'll convey it here.

Workaround Example
// I think this is a bug, to reference Coffee this way. See @typedef of 'Coffee' w/in the function
/** @typedef {Coffee} CaffeineDrink */

/**
 * @param {Boolean} cream
 * @param {Boolean} sugar
 * @returns {Coffee}
 */
export default function Coffee(cream, sugar) {
  const self = {
    privateProperties: {
      coffeeGrounds: {flavor: 'Pike\'s Peak'},
      coffeeMachine: () => {},
      makeCoffee: () => Math.random(),
    },
  };

  /**
   * I think this is a hacky/buggy way to specify the return type, using the function's name.
   * @typedef {drink} Coffee
   */
  /**
   * @member
   */
  const drink = {
    isCold: self.privateProperties.makeCoffee() > 0.5,
    hasCream: cream,
    hasSugar: sugar,
  };

  self.drink = drink;

  return self.drink;
}
Screenshot of Typehinting

image

The key points to make this workaround successful are

  1. Identifying the variable as a Member of the function
    • /**
       * @member
       */
      const drink = {}
      
  2. Creating a TypeDef who's name matches the parent function
    • /** @typedef {drink} Coffee */
      
  3. Finally, referencing the Function/TypeDef in a new TypeDef, for scope purposes.
    • /** @typedef {Coffee} CaffeineDrink */
      

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review how JSDoc currently resolves @returns and @typedef references, using the Coffee, Drink, and CaffeineDrink examples in the issue as the starting case. Compare the requested @returns-based reference with the documented workaround, and define completion as correctly deriving the typedef structure when the function return shape changes.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
documentation
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.