Skip to content

Commit c47aca0

Browse files
authored
Accurate Array.prototype.flat definition (#32131)
* Better Array.prototype.flat definition * Use more meaningful names * Rename 'Flat' to 'FlatArray'
1 parent 7ca6334 commit c47aca0

File tree

1 file changed

+16
-154
lines changed

1 file changed

+16
-154
lines changed

src/lib/es2019.array.d.ts

+16-154
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
type FlatArray<Arr, Depth extends number> = {
2+
"done": Arr,
3+
"recur": Arr extends ReadonlyArray<infer InnerArr>
4+
? FlatArray<InnerArr, [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20][Depth]>
5+
: Arr
6+
}[Depth extends -1 ? "done" : "recur"];
7+
18
interface ReadonlyArray<T> {
29

310
/**
@@ -22,95 +29,11 @@ interface ReadonlyArray<T> {
2229
*
2330
* @param depth The maximum recursion depth
2431
*/
25-
flat<U>(this:
26-
ReadonlyArray<U[][][][]> |
27-
28-
ReadonlyArray<ReadonlyArray<U[][][]>> |
29-
ReadonlyArray<ReadonlyArray<U[][]>[]> |
30-
ReadonlyArray<ReadonlyArray<U[]>[][]> |
31-
ReadonlyArray<ReadonlyArray<U>[][][]> |
32-
33-
ReadonlyArray<ReadonlyArray<ReadonlyArray<U[][]>>> |
34-
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>[][]>> |
35-
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>[][]> |
36-
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>[]>[]> |
37-
ReadonlyArray<ReadonlyArray<ReadonlyArray<U[]>>[]> |
38-
ReadonlyArray<ReadonlyArray<ReadonlyArray<U[]>[]>> |
39-
40-
ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U[]>>>> |
41-
ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>[]>>> |
42-
ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>[]>> |
43-
ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>[]> |
44-
45-
ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>>>,
46-
depth: 4): U[];
47-
48-
/**
49-
* Returns a new array with all sub-array elements concatenated into it recursively up to the
50-
* specified depth.
51-
*
52-
* @param depth The maximum recursion depth
53-
*/
54-
flat<U>(this:
55-
ReadonlyArray<U[][][]> |
56-
57-
ReadonlyArray<ReadonlyArray<U>[][]> |
58-
ReadonlyArray<ReadonlyArray<U[]>[]> |
59-
ReadonlyArray<ReadonlyArray<U[][]>> |
60-
61-
ReadonlyArray<ReadonlyArray<ReadonlyArray<U[]>>> |
62-
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>[]>> |
63-
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>[]> |
64-
65-
ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>>,
66-
depth: 3): U[];
67-
68-
/**
69-
* Returns a new array with all sub-array elements concatenated into it recursively up to the
70-
* specified depth.
71-
*
72-
* @param depth The maximum recursion depth
73-
*/
74-
flat<U>(this:
75-
ReadonlyArray<U[][]> |
76-
77-
ReadonlyArray<ReadonlyArray<U[]>> |
78-
ReadonlyArray<ReadonlyArray<U>[]> |
79-
80-
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>,
81-
depth: 2): U[];
82-
83-
/**
84-
* Returns a new array with all sub-array elements concatenated into it recursively up to the
85-
* specified depth.
86-
*
87-
* @param depth The maximum recursion depth
88-
*/
89-
flat<U>(this:
90-
ReadonlyArray<U[]> |
91-
ReadonlyArray<ReadonlyArray<U>>,
92-
depth?: 1
93-
): U[];
94-
95-
/**
96-
* Returns a new array with all sub-array elements concatenated into it recursively up to the
97-
* specified depth.
98-
*
99-
* @param depth The maximum recursion depth
100-
*/
101-
flat<U>(this:
102-
ReadonlyArray<U>,
103-
depth: 0
104-
): U[];
105-
106-
/**
107-
* Returns a new array with all sub-array elements concatenated into it recursively up to the
108-
* specified depth. If no depth is provided, flat method defaults to the depth of 1.
109-
*
110-
* @param depth The maximum recursion depth
111-
*/
112-
flat<U>(depth?: number): any[];
113-
}
32+
flat<A, D extends number = 1>(
33+
this: A,
34+
depth?: D
35+
): FlatArray<A, D>[]
36+
}
11437

11538
interface Array<T> {
11639

@@ -135,69 +58,8 @@ interface Array<T> {
13558
*
13659
* @param depth The maximum recursion depth
13760
*/
138-
flat<U>(this: U[][][][][][][][], depth: 7): U[];
139-
140-
/**
141-
* Returns a new array with all sub-array elements concatenated into it recursively up to the
142-
* specified depth.
143-
*
144-
* @param depth The maximum recursion depth
145-
*/
146-
flat<U>(this: U[][][][][][][], depth: 6): U[];
147-
148-
/**
149-
* Returns a new array with all sub-array elements concatenated into it recursively up to the
150-
* specified depth.
151-
*
152-
* @param depth The maximum recursion depth
153-
*/
154-
flat<U>(this: U[][][][][][], depth: 5): U[];
155-
156-
/**
157-
* Returns a new array with all sub-array elements concatenated into it recursively up to the
158-
* specified depth.
159-
*
160-
* @param depth The maximum recursion depth
161-
*/
162-
flat<U>(this: U[][][][][], depth: 4): U[];
163-
164-
/**
165-
* Returns a new array with all sub-array elements concatenated into it recursively up to the
166-
* specified depth.
167-
*
168-
* @param depth The maximum recursion depth
169-
*/
170-
flat<U>(this: U[][][][], depth: 3): U[];
171-
172-
/**
173-
* Returns a new array with all sub-array elements concatenated into it recursively up to the
174-
* specified depth.
175-
*
176-
* @param depth The maximum recursion depth
177-
*/
178-
flat<U>(this: U[][][], depth: 2): U[];
179-
180-
/**
181-
* Returns a new array with all sub-array elements concatenated into it recursively up to the
182-
* specified depth.
183-
*
184-
* @param depth The maximum recursion depth
185-
*/
186-
flat<U>(this: U[][], depth?: 1): U[];
187-
188-
/**
189-
* Returns a new array with all sub-array elements concatenated into it recursively up to the
190-
* specified depth.
191-
*
192-
* @param depth The maximum recursion depth
193-
*/
194-
flat<U>(this: U[], depth: 0): U[];
195-
196-
/**
197-
* Returns a new array with all sub-array elements concatenated into it recursively up to the
198-
* specified depth. If no depth is provided, flat method defaults to the depth of 1.
199-
*
200-
* @param depth The maximum recursion depth
201-
*/
202-
flat<U>(depth?: number): any[];
61+
flat<A, D extends number = 1>(
62+
this: A,
63+
depth?: D
64+
): FlatArray<A, D>[]
20365
}

0 commit comments

Comments
 (0)