typescripttypescript-generics

Field's type base on it parent's type


I'm facing the following problem in Typescript:

class PopulationDto<E> {
    path: keyof E;
    population?: PopulationDto<any>[];
}

class Building {
  floor: string;
  room: string;
}

class Address {
  building: Building;
  street: string;
  city: string;
}

class User {
  name: string;
  address: Address[];
}

const population: PopulationDto<User>[] = [
    { path: "address", population: [{ path: "building" }] },
];

I get type suggestion in the first path ("address" | "name"), and I also want the second one to suggest the corresponding type ("building" | "street" | "city"), but it doesn't work since the recursion type is any.

Is there any way to do it?


Solution

  • I found the answer here: https://github.com/microsoft/TypeScript/issues/36444#issuecomment-578572999

    I have to convert my class to type in order to achieve the solution. Something like this:

    type PopulationDto<E> = {
        [K in keyof E]: {
            path: keyof E;
            population?: PopulationDto<
                E[K] extends Array<any>
                    ? E[K][number]
                    : E[K] extends object
                      ? E[K]
                      : any
            >[];
        }
    }[keyof E];