错误:不能调用其类型从使用阵列缺乏呼叫签名的表达

问题描述 投票:0回答:3

我有一个数组这是一个有一堆在它的对象:

const standardStories = [
{
    name: 'Executive Summary',
    navLink: 'standard-stories/executive-summary',
    cardIndex: 0,
    storyBoxes: [
        {
            name: 'Chart Box',
            values: commonStdStories.chartBoxValue,
            graphics: commonStdStories.chartBoxGraphic,
        },
        {
            name: 'Financial Chart',
            values: commonStdStories.financialChartValue,
            graphics: commonStdStories.pieChart,
        },
        {
            name: 'Info Progress Bar',
            values: commonStdStories.progressBarValue,
            graphics: commonStdStories.progressBarGraphic,
        },
    ],
},
{
    name: 'High Cost Med',
    navLink: 'standard-stories/hcc-med',
    cardIndex: 2,
    storyBoxes: [
        {
            name: 'Info Progress Bar',
            values: commonStdStories.progressBarValue,
            graphics: commonStdStories.progressBarGraphic,
        },
        {
            name: 'Chart Box',
            values: commonStdStories.chartBoxValue,
            graphics: commonStdStories.chartBoxGraphic,
        },
        {
            name: 'HCC Table',
            value: commonStdStories.hccTable,
        },
    ],
},
{
    name: 'High Cost Rx',
    navLink: 'standard-stories/hcc-rx',
    cardIndex: 3,
    storyBoxes: [
        {
            name: 'Info Progress Bar',
            values: commonStdStories.progressBarValue,
            graphics: commonStdStories.progressBarGraphic,
        },
        {
            name: 'Chart Box',
            values: commonStdStories.chartBoxValue,
            graphics: commonStdStories.chartBoxGraphic,
        },
        {
            name: 'HCC Table',
            value: commonStdStories.hccTable,
        },
    ],
},
]

还有更多,但你得到的图片。

我试图使用storyBoxes像这样:

standardStories.forEach(story => {
    **story.storyBoxes.forEach(storyBox => {})**
})

在粗体线是我正在错误[ts] Cannot invoke an expression whose type lacks a call signature只是想知道如何解决这个问题,所以我可以继续前进。理想的情况是我希望能够遍历每个故事检查每个在storyBox每个故事概述的storyBoxes的。

typescript
3个回答
1
投票

这取决于内部commonStdStories的属性是什么类型。如果类型为storyBoxes内部的性质并不一致,storyBoxes的类型可能最终被阵列的联合,这是因为这些方法最终版本的是工会在每种类型不能作为数组使用联盟。

最简单的办法是增加一个明确的类型为恒,基于我的问题里面看到的,这将是一个版本:

interface IStory {

    name: string;
    navLink: string;
    cardIndex: number;
    storyBoxes: Array<{
        name: string;
        value?: string | number;
        values?: string| number; // Typo maybe ? do you want both value and values ?
        graphics?: string| number;
    }>;
}

Playground link


0
投票

所以,我在storyBoxes阵列在改变名称boxName并使value要和别人一样values解决了这个问题。


0
投票

这不再是打字稿3.2的情况下。现在,如果一个工会的每一个成员是可调用的,工会本身是可调用的为好。

© www.soinside.com 2019 - 2024. All rights reserved.