首页 > 解决方案 > TypeScript 错误:属性不存在 - 虽然它存在

问题描述

我有一个带有 TypeScript 的 React 应用程序。这是一个 Redux 选择器函数:

export const calendarImagesSelector = (state: AppState): Content[] =>

    Object.values(state.entities.content.byId)

        .filter((selectedImage) => selectedImage.type === 'image' && selectedImage?.imageInformation?.src)

        .map((image: Content) => image.imageInformation.src);

我得到错误Property 'imageInformation' does not exist on type 'Content'.

但是查看Content类型,我认为它在那里:

export type Content = ImageContent | TextContent;

export interface ImageContent extends BaseContent {
    type: ContentTypes.IMAGE;
    path: string;
    rotation: number;
    imageInformation?: ProductImage;
}

export interface TextContent extends BaseContent {
    type: ContentTypes.TEXT;
    fontSize: number;
    horizontalJustification: HorizontalJustification;
    verticalJustification: VerticalJustification;
    fontFamily: string;
    color: CMYK;
    textContent: string;
    rotation: number;
    displayAlign?: string;
    lineHeight?: string;
    originalTop?: string;
    readOnly?: string;
    notes?: string[];
}

我找到的唯一可能的解决方案是说.map((image: any) => ...);

但我当然不想要。

包括在内imageInformationTextContent无济于事。

因为那样它会给出另一个错误: Type 'string[]' is not assignable to type 'Content[]'.

标签: typescript

解决方案


推荐阅读