react-desc icon indicating copy to clipboard operation
react-desc copied to clipboard

New PropTypes.func that accepts argument/return configuration

Open msholty-fd opened this issue 6 years ago • 0 comments

Right now you can use PropTypes.func to describe a prop in propTypes, but for index.d.ts files generated for components this function really falls short.

I believe a more dynamic PropTypes.func would produce better documentation. It could be PropTypes.function.

For an example, FormField has a doc.js like this:

DocumentedFormField.propTypes = {
  validate: PropTypes.oneOfType([
      PropTypes.shape({
        regexp: PropTypes.object, // regular expression
        message: PropTypes.string,
      }),
      PropTypes.func,
    ])
  }
}

I believe with this new PropTypes.function, we could better describe the shape of the validate function as such:

  validate: PropTypes.oneOfType([
      PropTypes.shape({
        regexp: PropTypes.object, // regular expression
        message: PropTypes.string,
      }),
      PropTypes.function(
        PropTypes.string,
        PropTypes.any,
      ).returns(PropTypes.oneOf([string, undefined]),
    ])

This would improve the index.d.ts produced by changing validate's second type from ((...args: any[]) => any)

to (arg1: string, arg2: any) => string | undefined

I would find it interesting to work on this but not sure if the maintainers would support this. Please discuss this with me so I can learn if it's something you'd want!

msholty-fd avatar Jun 21 '19 13:06 msholty-fd