-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03243-medium-flattendepth.ts
30 lines (28 loc) · 1.02 KB
/
03243-medium-flattendepth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// ============= Test Cases =============
import type { Equal, Expect } from "./test-utils";
type cases = [
Expect<Equal<FlattenDepth<[]>, []>>,
Expect<Equal<FlattenDepth<[1, 2, 3, 4]>, [1, 2, 3, 4]>>,
Expect<Equal<FlattenDepth<[1, [2]]>, [1, 2]>>,
Expect<Equal<FlattenDepth<[1, 2, [3, 4], [[[5]]]], 2>, [1, 2, 3, 4, [5]]>>,
Expect<Equal<FlattenDepth<[1, 2, [3, 4], [[[5]]]]>, [1, 2, 3, 4, [[5]]]>>,
Expect<Equal<FlattenDepth<[1, [2, [3, [4, [5]]]]], 3>, [1, 2, 3, 4, [5]]>>,
Expect<
Equal<FlattenDepth<[1, [2, [3, [4, [5]]]]], 19260817>, [1, 2, 3, 4, 5]>
>
];
// ============= Your Code Here =============
type FlattenDepth<
T extends any[],
U extends number = 1,
Depth extends any[] = []
> = T extends [infer First, ...infer Rest]
? First extends unknown[]
? Depth["length"] extends U
? [First, ...FlattenDepth<Rest, U, Depth>]
: [
...FlattenDepth<First, U, [unknown, ...Depth]>,
...FlattenDepth<Rest, U, Depth>
]
: [First, ...FlattenDepth<Rest, U, Depth>]
: T;