I have a function that looks at the provided children and if a particular element type is found, it adds some properties to it automatically.
The function is called like this:
render () {
const { children, name, className } = this.props;
return (
<div className={className}>
{this.enrichRadioElements(children, name)}
</div>
)
}
and it is implemented like this:
enrichRadioElements = (children: Array<any>, name: string) => (
React.Children.map(children, child => {
if (!React.isValidElement(child)) {
return child;
}
//@ts-ignore
if (child.props.children) {
child = React.cloneElement(child, {
//@ts-ignore
children: this.enrichRadioElements(child.props.children, name)
});
}
if (child.type === Radio) {
return React.cloneElement(child, {
onChange: this.handleFieldChange,
selectedValue: this.state.selectedValue,
name: name
})
}
else {
return child;
}
})
)
The two //@ts-ignore
comments are what I'm trying to get rid of by writing code that will satisfy typescript. If I remove the first one, the error message I see is this:
Property 'children' does not exist on type '{}'.(ts-2339)
How can I properly modify my code so I can remove the //@ts-ignore
comments? I did go to the definition of child.props and I found this:
interface ReactElement<P = any, T extends string | JSXElementConstructor<any> = string | JSXElementConstructor<any>> {
type: T;
props: P;
key: Key | null;
}
which looks to have a 'props' of type any (if I'm reading it correctly), but typescript doesn't recognize the children property.