type Depth = 1 | 2 | 3 | 4 | 5 | 6; type NestedArray = D extends 1 ? T[] : D extends 2 ? T[][] : D extends 3 ? T[][][] : D extends 4 ? T[][][][] : D extends 5 ? T[][][][][] : T[][][][][][]; type PrevDepth = D extends 6 ? 5 : D extends 5 ? 4 : D extends 4 ? 3 : D extends 3 ? 2 : D extends 2 ? 1 : never; type LeafAtDepth = D extends 1 ? A extends (infer T)[] ? T : never : A extends (infer T)[] ? LeafAtDepth> : never; type LeafValue = FilterUndefined extends true ? Exclude : T; /** * Returns the result of mapping the array to the given depth and, optionally, * filtering out `undefined` leaf values. * * @param tss * @param f * @param filterUndefined */ function mapDeep, U, F extends boolean = false>( depth: D, tss: A, f: (t: LeafAtDepth) => U, filterUndefined?: F): NestedArray, D> { if (!Number.isInteger(depth) || depth < 1) { throw new Error('depth must be an integer >= 1'); } function mapRec(d: number, arr: unknown): unknown { const a = arr as unknown[]; if (d === 1) { const ts_: U[] = []; for (let i=0; i); if (!filterUndefined || v !== undefined) { ts_.push(v); } } return ts_; } const tss_: unknown[] = []; for (let i=0; i, D>; } export { mapDeep }