TP_Dev
TP_Dev

Reputation: 71

How do I create a type from another type's ReadonlyArray<Maybe<()> property?

I have the following type:

type TypeOne = {
    data: ReadonlyArray<Maybe<(
        { readonly __typename?: 'EventJson1' }
        & Pick<EventJson1, 'id' | 'start' | 'end' | 'timezone' | 'title' | 'status'>
        & { readonly planners: Maybe<ReadonlyArray<Maybe<(
          { readonly __typename?: 'PlannerJson1' }
          & Pick<PlannerJson1, 'firstName' | 'lastName'>
        )>>>, readonly customFields: Maybe<ReadonlyArray<Maybe<(
          { readonly __typename?: 'CustomFieldJson1' }
          & Pick<CustomFieldJson1, 'name' | 'value' | 'id' | 'type' | 'order'>
        )>>>
    })>>
};

I need to create a new type from this type that can hold an element from the data array.

I can explicitly create the correct type like this:

type TypeTwo = 
    { readonly __typename?: 'EventJson1' }
    & Pick<EventJson1, 'id' | 'start' | 'end' | 'timezone' | 'title' | 'status'>
    & { readonly planners: Maybe<ReadonlyArray<Maybe<(
      { readonly __typename?: 'PlannerJson1' }
      & Pick<PlannerJson1, 'firstName' | 'lastName'>
    )>>>, readonly customFields: Maybe<ReadonlyArray<Maybe<(
      { readonly __typename?: 'CustomFieldJson1' }
      & Pick<CustomFieldJson1, 'name' | 'value' | 'id' | 'type' | 'order'>
    )>>>
};

But I cannot figure out how to create TypeTwo in terms of TypeOne. I have tried the following:

type TypeTwo = TypeOne['data']

But this does not work. Can someone show me how to do this please?

Upvotes: 2

Views: 76

Answers (1)

TP_Dev
TP_Dev

Reputation: 71

type TypeTwo = TypeOne extends { data: ReadonlyArray<Maybe<infer X>>; } ? X : never;

Upvotes: 1

Related Questions