Search code examples
javascripttypescriptjsdoc

JsDoc type union on @returns attribute


This example is probably a weird pattern and I might change it anyway, but that lead me to wonder if it was possible to define a union type on the @returns attribute of JsDoc. Here is the example to make it more explicit :

  function isInputValid(email: string, password: string): string | void {
    /**
     * Verify user email and password inputs.
     * @param {string} email - The user email input.
     * @param {string} password - The user password input.
     * @returns {boolean | void} The invalid user input.
     */
    if (!validEmail(email)) {
      return email;
    }

    if (!validPassword(password)) {
      return password;
    }
  }

Solution

  • Refer to the JSDoc documentation on @return here.

    You were close, just missing some brackets ().

    /**
     * Verify user email and password inputs.
     * @param {string} email - The user email input.
     * @param {string} password - The user password input.
     * @returns {(boolean|void)} The invalid user input.
     */
    

    Hope this helps.