Search code examples
reactjstypescripttypescript-typingschildrenreact-props

What is the correct typescript type for react children?


I'm trying to properly type the props for a component that maps children:

type Props = {
    children: any
}

const MyComponent: FunctionComponent<Props> = () => (React.Children.map(children, someMapingFunction);

I've been using JSX.Element but that doesn't quite feel right.


Solution

  • Looking through the code under DefinitelyTyped it appears that children is typed as ReactNode.

    Example:

    type Props = {
        children: ReactNode
    }
    
    const MyComponent: FunctionComponent<Props> = () => (React.Children.map(children, someMapingFunction);
    

    Note: The ReactNode type can be found in the React namespace:

    import React from 'react';
    
    let someNode: React.ReactNode;