Reactjs 类型';{children:void;header:string;key:string;}';不可分配给类型';只读<;{children?:ReactNode;}>';

Reactjs 类型';{children:void;header:string;key:string;}';不可分配给类型';只读<;{children?:ReactNode;}>';,reactjs,typescript,Reactjs,Typescript,我得到以下错误。我对反应有点陌生,所以不知道如何应对 类型“{children:void;header:string;key:string;}”不可分配给类型“Readonly”。 属性“children”的类型不兼容。 类型“void”不能分配给类型“ReactNode”。 首先,我有两个变量: const recentPitches = this.props.lastPitched.forEach(pitch => <p> <small>

我得到以下错误。我对反应有点陌生,所以不知道如何应对

类型“{children:void;header:string;key:string;}”不可分配给类型“Readonly”。
属性“children”的类型不兼容。
类型“void”不能分配给类型“ReactNode”。

首先,我有两个变量:

const recentPitches = this.props.lastPitched.forEach(pitch =>
    <p>
        <small>
            {pitch.pitchingInitiativeName} ({pitch.accountName}) | <span className="text-primary">status</span><br />
            By {pitch.userName} at {moment(pitch.lastPitchTime).format('lll')}
        </small>
    </p>
);
const upcomingPitches = this.props.upcomingPitches.forEach(pitch =>
    <p>
        {pitch.pitchingInitiativeName} ({pitch.accountName}) | {moment(pitch.publishingDeadline).format('lll')}
    </p>
);
const-recentpitchs=this.props.lastprotched.forEach(pitch=>

{pitch.pitchingInitiativeName}({pitch.accountName})|状态
通过{pitch.userName}在{moment(pitch.lastPitchTime).format('lll')}

); const upComingPitchs=this.props.upComingPitchs.forEach(pitch=> {pitch.pitchingInitiativeName}({pitch.accountName}){moment(pitch.publishingDeadline).format('lll')}

);
然后我在我的render方法中调用它们,这就是导致错误的原因:

<Collapse>
    <Collapse.Panel header="Most Recent Pitches" key="1">
        {recentPitches}
    </Collapse.Panel>
    <Collapse.Panel header="Upcoming Pitches" key="2">
        {upcomingPitches}
    </Collapse.Panel>
</Collapse>

{recentpitchs}
{UpComingPitchs}

类型“{children:void;header:string;key:string;}”不能分配给类型“Readonly”

TypeScript推断子项(例如,
recentPitchs
)为
void
。发生这种情况的原因示例:

const foo = function(){}
const recentPitches = foo(); // void 
修理 我怀疑你想要一个
void
作为
折叠面板的子对象。至少这不是
Collapse.Panel
的类型所期望的,因此出现了错误

错误是有效的。错误在于你有
recentpitchs
作为
void
。解决了这个问题,错误就会迎刃而解

类型“{children:void;header:string;key:string;}”不能分配给类型“Readonly”

TypeScript推断子项(例如,
recentPitchs
)为
void
。发生这种情况的原因示例:

const foo = function(){}
const recentPitches = foo(); // void 
修理 我怀疑你想要一个
void
作为
折叠面板的子对象。至少这不是
Collapse.Panel
的类型所期望的,因此出现了错误


错误是有效的。错误在于你有
recentpitchs
作为
void
。解决这个问题,错误就会消失。

您可能想在
this.props.lastprotched
this.props.upcomingPitchs上使用
map
,而不是
forEach
Array.prototype.forEach
不返回任何内容,而
Array.prototype.map
将每个元素上回调的返回值收集到一个新数组中。

您可能想在
this.props.lastPitched
this.props.upComingPitchs
上使用
map
,而不是
forEach
Array.prototype.forEach
不返回任何内容,而
Array.prototype.map
将每个元素的回调返回值收集到一个新数组中。

感谢您的反馈,我理解您的意思,但是仍然有点不清楚如何修复它。@SandraWillford添加了另一句话来解释要修复的内容。好的,我在最近的音高和即将到来的音高中添加了一个any类型,这样就消除了错误。不过,我担心正确的类型应该是什么。谢谢你的反馈,我理解你的意思,但仍然有点不清楚如何解决它。@SandraWillford添加了另一句话来解释要解决的问题。好的,我在最近的音高和即将到来的音高中添加了一个any类型,这样就消除了错误。然而,我关心的是正确的类型到底应该是什么。