In my code, I'm trying to access the type property of a React.ReactElement array element. Even though I've defined the array to contain React.ReactElement children, I get an error trying to access an array element's type property unless I cast the array element to be of the type React.ReactElement.
Here's my code:
const getChildren = (
children: (React.ReactChild | React.ReactElement)[]
) => {
console.log((children[0] as React.ReactElement).type) // this line works
console.log(children[0].type) // this line breaks
}
and here's the error that I get:
Property 'type' does not exist on type 'ReactChild | ReactElement<any, string | JSXElementConstructor<any>>'.
Property 'type' does not exist on type 'string'. TS2339
How can I resolve this issue?