如何使用索引访问类型查找特定 属性?

How to look up specific property using indexed access type?

我有以下自动生成的代码:

export type TourItemQuery = {
  __typename?: 'Query';
  tourItem?: {
    __typename?: 'TourItem';
    id: number;
    name: string;
    images: Array<{
      __typename?: 'Image';
      id: number;
      url: string;
      blurhash: string;
    }>;
    itineraries: Array<{
      __typename?: 'ItineraryList';
      id: string;
      day?: number | null;
      polyline: string;
      pointOfInterestList: Array<{
        __typename?: 'PointOfInterestList';
        order: number;
        distanceFromPrevious?: number | null;
        durationFromPrevious?: number | null;
        pointOfInterest: {
          __typename?: 'PointOfInterest';
          id: number;
          name: string;
          location: {
            __typename?: 'Point';
            latitude: number;
            longitude: number;
          };
          images: Array<{
            __typename?: 'Image';
            id: number;
            url: string;
            blurhash: string;
          }>;
        };
      }>;
    }>;
  } | null;
};

如何访问 itineraries 类型?

这是我尝试过的:

type TourItem = TourItemQuery['tourItem']; // OK
type Itineraries = TourItemQuery['tourItem']['itineraries']; // NOK: "Property 'itineraries' does not exist on type {...}"

文档参考:https://www.typescriptlang.org/docs/handbook/2/indexed-access-types.html

TourItemQuery['tourItem']是包含nullundefined的联合类型,使用NonNullable将它们排除在联合

之外
type Itineraries = NonNullable<TourItemQuery['tourItem']>['itineraries'];