From ade67c6650d29e86623cf10552e5373b7cb94aa0 Mon Sep 17 00:00:00 2001 From: Bannerets Date: Thu, 27 Jun 2019 09:10:24 +0000 Subject: [PATCH 1/3] Better Array.prototype.flat definition --- src/lib/es2019.array.d.ts | 168 ++++---------------------------------- 1 file changed, 15 insertions(+), 153 deletions(-) diff --git a/src/lib/es2019.array.d.ts b/src/lib/es2019.array.d.ts index 33bee0c205efe..d10a20d18aa29 100644 --- a/src/lib/es2019.array.d.ts +++ b/src/lib/es2019.array.d.ts @@ -1,3 +1,10 @@ +type Flat = { + '1': A, + '0': A extends ReadonlyArray + ? Flat + : A +}[D extends -1 ? '1' : '0'] + interface ReadonlyArray { /** @@ -22,94 +29,10 @@ interface ReadonlyArray { * * @param depth The maximum recursion depth */ - flat(this: - ReadonlyArray | - - ReadonlyArray> | - ReadonlyArray[]> | - ReadonlyArray[][]> | - ReadonlyArray[][][]> | - - ReadonlyArray>> | - ReadonlyArray[][]>> | - ReadonlyArray>[][]> | - ReadonlyArray[]>[]> | - ReadonlyArray>[]> | - ReadonlyArray[]>> | - - ReadonlyArray>>> | - ReadonlyArray[]>>> | - ReadonlyArray>[]>> | - ReadonlyArray>>[]> | - - ReadonlyArray>>>>, - depth: 4): U[]; - - /** - * Returns a new array with all sub-array elements concatenated into it recursively up to the - * specified depth. - * - * @param depth The maximum recursion depth - */ - flat(this: - ReadonlyArray | - - ReadonlyArray[][]> | - ReadonlyArray[]> | - ReadonlyArray> | - - ReadonlyArray>> | - ReadonlyArray[]>> | - ReadonlyArray>[]> | - - ReadonlyArray>>>, - depth: 3): U[]; - - /** - * Returns a new array with all sub-array elements concatenated into it recursively up to the - * specified depth. - * - * @param depth The maximum recursion depth - */ - flat(this: - ReadonlyArray | - - ReadonlyArray> | - ReadonlyArray[]> | - - ReadonlyArray>>, - depth: 2): U[]; - - /** - * Returns a new array with all sub-array elements concatenated into it recursively up to the - * specified depth. - * - * @param depth The maximum recursion depth - */ - flat(this: - ReadonlyArray | - ReadonlyArray>, - depth?: 1 - ): U[]; - - /** - * Returns a new array with all sub-array elements concatenated into it recursively up to the - * specified depth. - * - * @param depth The maximum recursion depth - */ - flat(this: - ReadonlyArray, - depth: 0 - ): U[]; - - /** - * Returns a new array with all sub-array elements concatenated into it recursively up to the - * specified depth. If no depth is provided, flat method defaults to the depth of 1. - * - * @param depth The maximum recursion depth - */ - flat(depth?: number): any[]; + flat( + this: A, + depth?: D + ): Flat[] } interface Array { @@ -135,69 +58,8 @@ interface Array { * * @param depth The maximum recursion depth */ - flat(this: U[][][][][][][][], depth: 7): U[]; - - /** - * Returns a new array with all sub-array elements concatenated into it recursively up to the - * specified depth. - * - * @param depth The maximum recursion depth - */ - flat(this: U[][][][][][][], depth: 6): U[]; - - /** - * Returns a new array with all sub-array elements concatenated into it recursively up to the - * specified depth. - * - * @param depth The maximum recursion depth - */ - flat(this: U[][][][][][], depth: 5): U[]; - - /** - * Returns a new array with all sub-array elements concatenated into it recursively up to the - * specified depth. - * - * @param depth The maximum recursion depth - */ - flat(this: U[][][][][], depth: 4): U[]; - - /** - * Returns a new array with all sub-array elements concatenated into it recursively up to the - * specified depth. - * - * @param depth The maximum recursion depth - */ - flat(this: U[][][][], depth: 3): U[]; - - /** - * Returns a new array with all sub-array elements concatenated into it recursively up to the - * specified depth. - * - * @param depth The maximum recursion depth - */ - flat(this: U[][][], depth: 2): U[]; - - /** - * Returns a new array with all sub-array elements concatenated into it recursively up to the - * specified depth. - * - * @param depth The maximum recursion depth - */ - flat(this: U[][], depth?: 1): U[]; - - /** - * Returns a new array with all sub-array elements concatenated into it recursively up to the - * specified depth. - * - * @param depth The maximum recursion depth - */ - flat(this: U[], depth: 0): U[]; - - /** - * Returns a new array with all sub-array elements concatenated into it recursively up to the - * specified depth. If no depth is provided, flat method defaults to the depth of 1. - * - * @param depth The maximum recursion depth - */ - flat(depth?: number): any[]; + flat( + this: A, + depth?: D + ): Flat[] } From 1b31861d5b361df055db8072de93945f736e6477 Mon Sep 17 00:00:00 2001 From: Bannerets Date: Thu, 27 Feb 2020 03:48:06 +0000 Subject: [PATCH 2/3] Use more meaningful names --- src/lib/es2019.array.d.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/lib/es2019.array.d.ts b/src/lib/es2019.array.d.ts index 761d4a011082b..f1a4d20e52f65 100644 --- a/src/lib/es2019.array.d.ts +++ b/src/lib/es2019.array.d.ts @@ -1,9 +1,9 @@ -type Flat = { - '1': A, - '0': A extends ReadonlyArray - ? Flat - : A -}[D extends -1 ? '1' : '0'] +type Flat = { + "done": Arr, + "recur": Arr extends ReadonlyArray + ? Flat + : Arr +}[Depth extends -1 ? "done" : "recur"]; interface ReadonlyArray { From c92d25f93b4a189cb370b505d61c67511fda8824 Mon Sep 17 00:00:00 2001 From: Bannerets Date: Fri, 27 Mar 2020 01:19:58 +0000 Subject: [PATCH 3/3] Rename 'Flat' to 'FlatArray' --- src/lib/es2019.array.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/es2019.array.d.ts b/src/lib/es2019.array.d.ts index f1a4d20e52f65..5181677c5f973 100644 --- a/src/lib/es2019.array.d.ts +++ b/src/lib/es2019.array.d.ts @@ -1,7 +1,7 @@ -type Flat = { +type FlatArray = { "done": Arr, "recur": Arr extends ReadonlyArray - ? Flat + ? FlatArray : Arr }[Depth extends -1 ? "done" : "recur"]; @@ -32,7 +32,7 @@ interface ReadonlyArray { flat( this: A, depth?: D - ): Flat[] + ): FlatArray[] } interface Array { @@ -61,5 +61,5 @@ interface Array { flat( this: A, depth?: D - ): Flat[] + ): FlatArray[] }