tsdoc icon indicating copy to clipboard operation
tsdoc copied to clipboard

Discussion: How to document curried functions using TSDoc

Open beliayeu opened this issue 5 years ago • 2 comments

Does tsdoc support curried functions? Is there an example how a curried function can be documented?

Related tickets: https://github.com/jsdoc/jsdoc/issues/1286 https://github.com/TypeStrong/typedoc/issues/1098

beliayeu avatar Jun 02 '20 11:06 beliayeu

It also seems related to this JSDoc issue: https://github.com/jsdoc/jsdoc/issues/1286

And this more general TSDoc discussion: https://github.com/microsoft/tsdoc/issues/241

I have not thought about this much myself.

It would be helpful for someone to:

  • share realistic examples of curried functions (with complete definitions for any other functions involved)
  • propose some syntaxes for how TSDoc comments could be used to document them

octogonz avatar Jun 05 '20 00:06 octogonz

Cross post from: https://github.com/jsdoc/jsdoc/issues/1286#issuecomment-652578072

This is the only solution that worked for me:

const { curry, pathSatisfies } = require('ramda');

/**
 * Checks whether the given predicate is satisfied for the property value
 * at `propName` under `lambdaEvent.requestContext.authorizer`.
 *
 * @function
 * @see https://ramdajs.com/docs/#pathSatisfies
 * @param {Function} predicate The boolean returning function to apply to the
 *  value of the `propName` property.
 * @param {String} propName The name of the property to evaluate.
 * @returns {Boolean} The result of invoking `predicate` with the value of the
 *  property named `propName` on the `lambdaEvent.requestContext.authorizer` object.
 */

const authorizerPropSatisfies = curry(
  /** @type {(predicate: function, propName: string) => boolean} */
  (predicate, propName) => pathSatisfies(predicate, ['lambdaEvent', 'requestContext', 'authorizer', propName]),
);

module.exports = authorizerPropSatisfies;
Screen Shot 2020-07-01 at 1 38 40 PM

The tradeoff: You have to declare types twice, once inside jsdoc and once just above the function declaration (and inside curry())

However, this doesn't feel completely unnatural as it resembles function declarations found in many FP languages

simonwjackson avatar Jul 01 '20 18:07 simonwjackson