Javascript 财产';指数';不存在于类型';{}'; if(this.props.children){ 返回React.Children.map(this.props.Children,(child,i)=>{ if(React.isValidElement(子项)){ 返回React.cloneElement(子元素为React.ReactElement{ index:child.props.index | | i/

Javascript 财产';指数';不存在于类型';{}'; if(this.props.children){ 返回React.Children.map(this.props.Children,(child,i)=>{ if(React.isValidElement(子项)){ 返回React.cloneElement(子元素为React.ReactElement{ index:child.props.index | | i/,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,试试这个 if (this.props.children) { return React.Children.map(this.props.children, (child, i) => { if (React.isValidElement(child)) { return React.cloneElement(child as React.ReactElement<any>, { index: ch

试试这个

if (this.props.children) {
    return React.Children.map(this.props.children, (child, i) => {
        if (React.isValidElement(child)) {
            return React.cloneElement(child as React.ReactElement<any>, {
                index: child.props.index || i // <-- line that causes error
            });
        }
        return child;
    });
}
注:
typescript
中,您无法使用点符号访问索引属性。因此它将产生编译时错误

您确定该子级具有道具吗?您可以尝试检查道具是否已设置吗?类似于
index:child.props?child.props.index:i
@Stretch0的东西只会产生相同的错误。请尝试
child.props['index']
instead@Madhavan.V谢谢,这很有效!将其作为答案发布,我会标记:)仍然非常困惑是什么导致了这一点…再次感谢。您是否有指向某个文档页面的链接来更详细地解释这一点?我已经在某个地方读到了这一点,但我不确定在哪里。我肯定会在收到此链接后在此处更新。:)
child.props['index']