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类型,这样就消除了错误。然而,我关心的是正确的类型到底应该是什么。