I am working with the following data type:
export type GetWeeklyArticleQuery = {
__typename?: 'Query';
weeklyArticle?: {
__typename?: 'WeeklyArticle';
date: any;
tags: Array<string>;
title: string;
authors: Array<{ __typename?: 'Author'; name: string; slug: string }>;
bodyContent: { __typename?: 'RichText'; html: string };
coverImage: {
__typename?: 'Asset';
url: string;
title?: string | null;
altText?: string | null;
description?: { __typename?: 'RichText'; html: string } | null;
caption?: { __typename?: 'RichText'; html: string } | null;
};
introLine: { __typename?: 'RichText'; html: string };
footnote: Array<{ __typename?: 'RichText'; html: string }>;
} | null;
};
In my code, I need to access the authors field but I am unsure how to handle it without defining a custom type like Array<{ __typename?}. In my code, I attempted the following:
let authors:weeklyArticleDto['authors'];
This results in an error in my VSCode editor and in TypeScript playground:
type weeklyArticleDto = GetWeeklyArticleQuery['weeklyArticle'];
const weeklyArticle: weeklyArticleDto = data.weeklyArticle;
let authors:weeklyArticleDto['authors'];
if (weeklyArticle) {
authors = weeklyArticle.authors;
}
How can I resolve this issue?