• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1type 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
8interface ReadonlyArray<T> {
9
10    /**
11     * Calls a defined callback function on each element of an array. Then, flattens the result into
12     * a new array.
13     * This is identical to a map followed by flat with depth 1.
14     *
15     * @param callback A function that accepts up to three arguments. The flatMap method calls the
16     * callback function one time for each element in the array.
17     * @param thisArg An object to which the this keyword can refer in the callback function. If
18     * thisArg is omitted, undefined is used as the this value.
19     */
20    flatMap<U, This = undefined> (
21        callback: (this: This, value: T, index: number, array: T[]) => U | ReadonlyArray<U>,
22        thisArg?: This
23    ): U[]
24
25
26    /**
27     * Returns a new array with all sub-array elements concatenated into it recursively up to the
28     * specified depth.
29     *
30     * @param depth The maximum recursion depth
31     */
32    flat<A, D extends number = 1>(
33        this: A,
34        depth?: D
35    ): FlatArray<A, D>[]
36  }
37
38interface Array<T> {
39
40    /**
41     * Calls a defined callback function on each element of an array. Then, flattens the result into
42     * a new array.
43     * This is identical to a map followed by flat with depth 1.
44     *
45     * @param callback A function that accepts up to three arguments. The flatMap method calls the
46     * callback function one time for each element in the array.
47     * @param thisArg An object to which the this keyword can refer in the callback function. If
48     * thisArg is omitted, undefined is used as the this value.
49     */
50    flatMap<U, This = undefined> (
51        callback: (this: This, value: T, index: number, array: T[]) => U | ReadonlyArray<U>,
52        thisArg?: This
53    ): U[]
54
55    /**
56     * Returns a new array with all sub-array elements concatenated into it recursively up to the
57     * specified depth.
58     *
59     * @param depth The maximum recursion depth
60     */
61    flat<A, D extends number = 1>(
62        this: A,
63        depth?: D
64    ): FlatArray<A, D>[]
65}
66