Is it possible document with jsdoc children or render props used as a function?

2.3k Views Asked by At

I'm trying to create a wrapper component using the react render pattern, but I also want to document the arguments passed through render/children, in order to have, for example, an helpful intellisense.

I tried to define my own component as React.ExoticComponent<React.ConsumerProps<MYTYPE>> but doing this it means declaring the component like a <Context.Consumer>, hiding the input props.

const Wrapper = ({children}) => {

    const exampleFunction = () => {} 

    return (
        <div>
            {children({exampleFunction})}
        </div>
    )
}

const ImplementationComponent = () => {

    const exampleFunction = () => {} 

    return (
        <Wrapper>
            {({exampleFunction}) => (
                // <Components...>
            )}
        </Wrapper>
    )
}

I want the typechecking in the implementation, in order to help who shall use the wrapper component.

2

There are 2 best solutions below

0
On BEST ANSWER
/** @param {{ children: JSX.Element}} [Props] */

const Wrapper = ({children}) => {...}
0
On

If you want to support string, null, nothing (empty), etc. use React.ReactNode and make it optional:

/**
 * @param {Object} props
 * @param {React.ReactNode} [props.children]
 * @returns {JSX.Element}
 */
function Wrapper ({children}) {
  // ...
})

Because:

type ReactNode = ReactElement | string | number | ReactFragment | ReactPortal | boolean | null | undefined;

But:

namespace JSX {
    interface Element extends React.ReactElement<any, any> { }
    ...

See: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts