lib.es2019.array.d.ts 3.2 KB

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