I've got Laravel backend and I use transformers. That means that I can type ?include=reviews_count,author, etc. to get additional data from backend. Is there any way to statically type it?
One way is, of course, to create one DTO type and all includes would be optional, but that's not ideal.
Other (the one I came up with) is to have some kind of generic type that would take DTO as a first argument and union of include types as a second argument. However I ran into Typescript design limitation issue because Typescript doesn't work this kind of indexing by a generic type.
The last thing I came up with to have some kind of Includes type that looks like this
export type Includes<BaseClass, Includes> = {
[T in keyof BaseClass]: BaseClass[T];
} &
{
[T in keyof Includes]: Includes[T];
};
type TeacherDTO = {
name: string;
surname: string;
}
type TeacherIncludeNumber = {
count: number;
}
type GetTeachersResponse = Includes<TeacherDTO, TeacherIncludeMember | ...>;
But it does not really work. Is there any reliable way to handle it?