Ошибка TypeScript: свойство не существует, хотя оно есть - PullRequest
0 голосов
/ 07 августа 2020

У меня есть приложение React с TypeScript. Это функция селектора 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) => ...);

Но, конечно, этого я не хочу.

Включение imageInformation в TextContent также не помогает.

Потому что тогда выдаст другую ошибку: Type 'string[]' is not assignable to type 'Content[]'.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...