type AnyArray = Array | ReadonlyArray; type ObjectMapDeep = { // A key must be in the query object to be added [key in keyof Obj]: Obj[key] extends AnyArray ? Array> : // If it's an object, pick it Exclude extends object ? // Check if the object is optional. If it is, we need to unwrap the undefined object // and then rewrap it (to keep the `undefined` types Obj[key] extends Exclude ? MapDeep : MapDeep | undefined : MappedType; }; export type MapDeep = Obj extends object ? ObjectMapDeep : Obj;