type RecObj = T[K] extends Array ? T : never const EMPTY_ARRAY = new Array(0) export function getEmptyArray() { return EMPTY_ARRAY } export function deepFilterRec( arr: Array>, recKey: K, filterFn: (item: T) => boolean ) { return arr.reduce( (acc, elem) => { if (!filterFn(elem)) return acc if (recKey in elem) { const newSubitems = deepFilterRec(elem[recKey] as Array>, recKey, filterFn) const newElem = { ...elem, [recKey]: newSubitems } if (newSubitems.length > 0 || filterFn(elem)) acc.push(newElem) } else { acc.push(elem) } return acc }, [] as Array> ) } export async function pluckPromise(promise: Promise, key: K) { const data = await promise return data[key] }