-
Notifications
You must be signed in to change notification settings - Fork 12.8k
feat(es2018): add definitions for Array.prototype.flatten and Array.prototype.flatMap #20431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
/// <reference path="lib.es2017.d.ts" /> | ||
/// <reference path="lib.es2017.d.ts" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
interface ReadonlyArray<T> { | ||
|
||
/** | ||
* Calls a defined callback function on each element of an array. Then, flattens the result into | ||
* a new array. | ||
* This is identical to a map followed by a flatten of depth 1. | ||
* | ||
* @param callback A function that accepts up to three arguments. The flatMap method calls the | ||
* callback function one time for each element in the array. | ||
* @param thisArg An object to which the this keyword can refer in the callback function. If | ||
* thisArg is omitted, undefined is used as the this value. | ||
*/ | ||
flatMap<U, This = undefined> ( | ||
callback: (this: This, value: T, index: number, array: T[]) => U|U[], | ||
thisArg?: This | ||
): 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 | ||
*/ | ||
flatten<U>(this: | ||
ReadonlyArray<U[][][][]> | | ||
|
||
ReadonlyArray<ReadonlyArray<U[][][]>> | | ||
ReadonlyArray<ReadonlyArray<U[][]>[]> | | ||
ReadonlyArray<ReadonlyArray<U[]>[][]> | | ||
ReadonlyArray<ReadonlyArray<U>[][][]> | | ||
|
||
ReadonlyArray<ReadonlyArray<ReadonlyArray<U[][]>>> | | ||
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>[][]>> | | ||
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>[][]> | | ||
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>[]>[]> | | ||
ReadonlyArray<ReadonlyArray<ReadonlyArray<U[]>>[]> | | ||
ReadonlyArray<ReadonlyArray<ReadonlyArray<U[]>[]>> | | ||
|
||
ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U[]>>>> | | ||
ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>[]>>> | | ||
ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>[]>> | | ||
ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>[]> | | ||
|
||
ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<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 | ||
*/ | ||
flatten<U>(this: | ||
ReadonlyArray<U[][][]> | | ||
|
||
ReadonlyArray<ReadonlyArray<U>[][]> | | ||
ReadonlyArray<ReadonlyArray<U[]>[]> | | ||
ReadonlyArray<ReadonlyArray<U[][]>> | | ||
|
||
ReadonlyArray<ReadonlyArray<ReadonlyArray<U[]>>> | | ||
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>[]>> | | ||
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>[]> | | ||
|
||
ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<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 | ||
*/ | ||
flatten<U>(this: | ||
ReadonlyArray<U[][]> | | ||
|
||
ReadonlyArray<ReadonlyArray<U[]>> | | ||
ReadonlyArray<ReadonlyArray<U>[]> | | ||
|
||
ReadonlyArray<ReadonlyArray<ReadonlyArray<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 | ||
*/ | ||
flatten<U>(this: | ||
ReadonlyArray<U[]> | | ||
ReadonlyArray<ReadonlyArray<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 | ||
*/ | ||
flatten<U>(this: | ||
ReadonlyArray<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, flatten method defaults to the depth of 1. | ||
* | ||
* @param depth The maximum recursion depth | ||
*/ | ||
flatten<U>(depth?: number): any[]; | ||
} | ||
|
||
interface Array<T> { | ||
|
||
/** | ||
* Calls a defined callback function on each element of an array. Then, flattens the result into | ||
* a new array. | ||
* This is identical to a map followed by a flatten of depth 1. | ||
* | ||
* @param callback A function that accepts up to three arguments. The flatMap method calls the | ||
* callback function one time for each element in the array. | ||
* @param thisArg An object to which the this keyword can refer in the callback function. If | ||
* thisArg is omitted, undefined is used as the this value. | ||
*/ | ||
flatMap<U, This = undefined> ( | ||
callback: (this: This, value: T, index: number, array: T[]) => U|U[], | ||
thisArg?: This | ||
): 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 | ||
*/ | ||
flatten<U>(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 | ||
*/ | ||
flatten<U>(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 | ||
*/ | ||
flatten<U>(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 | ||
*/ | ||
flatten<U>(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 | ||
*/ | ||
flatten<U>(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 | ||
*/ | ||
flatten<U>(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 | ||
*/ | ||
flatten<U>(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 | ||
*/ | ||
flatten<U>(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, flatten method defaults to the depth of 1. | ||
* | ||
* @param depth The maximum recursion depth | ||
*/ | ||
flatten<U>(depth?: number): any[]; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/// <reference path="lib.es2018.d.ts" /> | ||
/// <reference path="lib.esnext.asynciterable.d.ts" /> | ||
/// <reference path="lib.esnext.array.d.ts" /> | ||
/// <reference path="lib.esnext.promise.d.ts" /> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will, unfortunately, do the wrong thing if
U
is deduced to be anArray
, but I'm not sure if there's a way of doing<U extends anything but Array>
. It's a place where the user will have to be careful.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Kovensky Indeed. However, I don't know either how we can achieve
<U extends anything by Array>
.Meanwhile, we can change the definition to
flatMap<U, This = undefined> ( callback: (this: This, value: T, index: number, array: T[]) =>
U,thisArg?: This ): U[];
I will be available to continue working on this PR starting from December 15th. Thanks for the feedback!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better if the callback's return is
U[]
instead ofU
, otherwise this looks no different, type-wise, from a regular.map
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Kovensky according to #20724, using conditional types it will be possible to define a type that extends anything but array.
With something like this I guess
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Kovensky for now, I will update the callback's return as
U[]
as you suggested