lib.es2019.array.d.ts (3163B)
1 /*! ***************************************************************************** 2 Copyright (c) Microsoft Corporation. All rights reserved. 3 Licensed under the Apache License, Version 2.0 (the "License"); you may not use 4 this file except in compliance with the License. You may obtain a copy of the 5 License at http://www.apache.org/licenses/LICENSE-2.0 6 7 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 8 KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 9 WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 10 MERCHANTABLITY OR NON-INFRINGEMENT. 11 12 See the Apache Version 2.0 License for specific language governing permissions 13 and limitations under the License. 14 ***************************************************************************** */ 15 16 17 /// <reference no-default-lib="true"/> 18 19 type FlatArray<Arr, Depth extends number> = { 20 done: Arr; 21 recur: Arr extends ReadonlyArray<infer InnerArr> ? 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]> 22 : Arr; 23 }[Depth extends -1 ? "done" : "recur"]; 24 25 interface ReadonlyArray<T> { 26 /** 27 * Calls a defined callback function on each element of an array. Then, flattens the result into 28 * a new array. 29 * This is identical to a map followed by flat with depth 1. 30 * 31 * @param callback A function that accepts up to three arguments. The flatMap method calls the 32 * callback function one time for each element in the array. 33 * @param thisArg An object to which the this keyword can refer in the callback function. If 34 * thisArg is omitted, undefined is used as the this value. 35 */ 36 flatMap<U, This = undefined>( 37 callback: (this: This, value: T, index: number, array: T[]) => U | ReadonlyArray<U>, 38 thisArg?: This, 39 ): U[]; 40 41 /** 42 * Returns a new array with all sub-array elements concatenated into it recursively up to the 43 * specified depth. 44 * 45 * @param depth The maximum recursion depth 46 */ 47 flat<A, D extends number = 1>( 48 this: A, 49 depth?: D, 50 ): FlatArray<A, D>[]; 51 } 52 53 interface Array<T> { 54 /** 55 * Calls a defined callback function on each element of an array. Then, flattens the result into 56 * a new array. 57 * This is identical to a map followed by flat with depth 1. 58 * 59 * @param callback A function that accepts up to three arguments. The flatMap method calls the 60 * callback function one time for each element in the array. 61 * @param thisArg An object to which the this keyword can refer in the callback function. If 62 * thisArg is omitted, undefined is used as the this value. 63 */ 64 flatMap<U, This = undefined>( 65 callback: (this: This, value: T, index: number, array: T[]) => U | ReadonlyArray<U>, 66 thisArg?: This, 67 ): U[]; 68 69 /** 70 * Returns a new array with all sub-array elements concatenated into it recursively up to the 71 * specified depth. 72 * 73 * @param depth The maximum recursion depth 74 */ 75 flat<A, D extends number = 1>( 76 this: A, 77 depth?: D, 78 ): FlatArray<A, D>[]; 79 }