2

i'm pretty new in typescript and can't figure out how i can modify deep nested interfaces.

I have interface ( which generated from graphql schema ) and i want to transform it to new model

it looks like so

interface SeatMap{
    // other fields
    segments: {
        // other fields
        rows: { // <-- i want to add one more field to row type
            // other fields
            seats: { // <-- i want to add few new fields to seat type
                isAvailable: boolean;
                isAisle: boolean;
                // etc.
            }
        }
    }
}

Than, i'm create two new functions

addServiceFieldToSeats which receive SeatMap and return SeatMap with modified seat type

and addRowPartsToRows which receive SeatMap and return SeatMap with modified row type

then via compose function from redux to create new function seatMapAdapter but can't figure out how to solve typescript errors

enter image description here

i know that my function transform seatmap rows type to new type RowWithParts but addServiceFieldToSeats receive old SeatMap type

How i can describe every SeatMap transformation?

2
  • Hello. Sorry I really don't understand your question. You have a generated interface, but you want to modify it? Why don't you just open the file and edit the file? Commented Nov 23, 2019 at 15:19
  • Because this interface is auto generated from graphql schema and if i modify generated file, it returns to previous state after npm generate interfaces command. Commented Nov 23, 2019 at 18:14

1 Answer 1

1

After few days of attempts, it looks like i find solution:

type NestedModelTransition<OriginalType, Compare, ReplaceWith> = {
    [Key in keyof OriginalType]: OriginalType[Key] extends Compare
        ? ReplaceWith
        : NestedModelTransition<OriginalType[Key], Compare, ReplaceWith>
};

type SeatMapWithSeatService = NestedModelTransition<SeatMap_SeatMap, SeatMap_SeatMap_segments_decks_rows_seats, Seat>;

type SeatMapWithRowWithParts = NestedModelTransition<
    SeatMapWithSeatService,
    SeatMap_SeatMap_segments_decks_rows,
    RowWithParts
>;
Sign up to request clarification or add additional context in comments.

1 Comment

I think this is missing some parts to really flesh out what's happening.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.