import React from 'react' ; export interface ChildrenProps < T extends Element , P extends React . HTMLProps < T > > { children : React . ReactNode ; defaultProps : P ; } export const childrenWithDefaultProps = < E extends Element , P extends any > ( props ) => React . Children . map < React . ReactNode , React . ReactNode > ( props . children , node => { if ( typeof node === 'string' ) { return node ; } if ( ! React . isValidElement < P > ( node ) ) { console . error ( 'Invalid children' , node ) ; return node ; } const elementChild = node ; return React . cloneElement < P > ( elementChild , { ... ( props . defaultProps as any ) , ... ( elementChild . props as any ) } ) ; } ) ;