代码之家  ›  专栏  ›  技术社区  ›  Sandra Willford

类型{children:void;表头:字符串;键:字符串;}'不可分配给类型'Readonly<{children?:ReactNode;}>'

  •  0
  • Sandra Willford  · 技术社区  · 8 年前

    Type '{ children: void; header: string; key: string; }' is not assignable to type 'Readonly<{ children?: ReactNode; }>'. Types of property 'children' are incompatible. Type 'void' is not assignable to type '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>
    );
    

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

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

    enter image description here

    2 回复  |  直到 8 年前
        1
  •  6
  •   Matt McCutchen    8 年前

    map ,不是 forEach ,开 this.props.lastPitched this.props.upcomingPitches . Array.prototype.forEach Array.prototype.map 将每个元素的回调返回值收集到一个新数组中。

        2
  •  0
  •   basarat    8 年前

    Type '{ children: void; header: string; key: string; }' is not assignable to type 'Readonly<{ children?: ReactNode; }>'

    TypeScript推断出。 recentPitches void . 发生这种情况的原因示例:

    const foo = function(){}
    const recentPitches = foo(); // void 
    

    修复

    我怀疑你想要一个 无效 作为孩子 Collapse.Panel . 至少那不是这些类型的用途 折叠面板 是预期的,因此出现错误。

    错误有效。问题是你有 最近音高 作为 . 解决这个问题,错误就会迎刃而解。

    推荐文章