-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3243-flattendepth.ts
36 lines (32 loc) · 1.34 KB
/
3243-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
31
32
33
34
35
36
/**
* 3243 - FlattenDepth
*
* Recursively flatten array up to depth times.
*
* For example:
*
* ```typescript
* type a = FlattenDepth<[1, 2, [3, 4], [[[5]]]], 2> // [1, 2, 3, 4, [5]]. flattern 2 times
* type b = FlattenDepth<[1, 2, [3, 4], [[[5]]]]> // [1, 2, 3, 4, [[5]]]. Depth defaults to be 1
* ```
*/
/* _____________ Your Code Here _____________ */
type FlattenDepth<L extends unknown[], Depth extends number = 1, CurrentDepthLevel extends number[] = []> =
Depth extends CurrentDepthLevel['length']
? L
: L extends [infer A, ...infer Rest]
? A extends unknown[]
? [...FlattenDepth<A, Depth, [...CurrentDepthLevel, 0]>, ...FlattenDepth<Rest, Depth, CurrentDepthLevel>]
: [A, ...FlattenDepth<Rest, Depth, CurrentDepthLevel>]
: L
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/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]>>,
]