microsoft / tsdoc

A doc comment standard for TypeScript
https://tsdoc.org/
MIT License
4.71k stars 130 forks source link

Discussion: How to document curried functions using TSDoc #240

Open beliayeu opened 4 years ago

beliayeu commented 4 years ago

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

octogonz commented 4 years ago

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:

simonwjackson commented 4 years ago

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