import type { ReactElement , ReactNode } from 'react' ; import { Children , isValidElement } from 'react' ; const hasChildren = ( element ) : element is ReactElement < { children : ReactNode [ ] ; } > => isValidElement < { children ? : ReactNode [ ] ; } > ( element ) && Boolean ( element . props . children ) ; const childToString = ( child ? ) : string => { if ( typeof child === 'undefined' || child === null || typeof child === 'boolean' ) { return '' ; } if ( JSON . stringify ( child ) === '{}' ) { return '' ; } return ( child as number | string ) . toString ( ) ; } ; export const getTextFromReactChildren = ( children ) : string => { if ( ! ( children instanceof Array ) && ! isValidElement ( children ) ) { return childToString ( children ) ; } return Children . toArray ( children ) . reduce ( ( text , child ) : string => { let newText = '' ; if ( isValidElement ( child ) && hasChildren ( child ) ) { newText = getTextFromReactChildren ( child . props . children ) ; } else if ( isValidElement ( child ) && ! hasChildren ( child ) ) { newText = '' ; } else { newText = childToString ( child ) ; } return text . concat ( newText ) ; } , '' ) as string ; } ;