Skip to content

Commit 804eb32

Browse files
benbraoumhegazy
authored andcommitted
feat(es2018): add definitions for Array.prototype.flatten and Array.prototype.flatMap (#20431)
* bug(esnext): add definitions for flatten and flatMap Fixes #20410 * bug(esnext) add overloads for flatten
1 parent 00d4aca commit 804eb32

8 files changed

+215
-8
lines changed

Gulpfile.ts

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ const es2018LibrarySourceMap = es2018LibrarySource.map(source =>
151151

152152
const esnextLibrarySource = [
153153
"esnext.asynciterable.d.ts",
154+
"esnext.array.d.ts",
154155
"esnext.promise.d.ts"
155156
];
156157

Jakefile.js

+1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ var es2018LibrarySourceMap = es2018LibrarySource.map(function (source) {
214214

215215
var esnextLibrarySource = [
216216
"esnext.asynciterable.d.ts",
217+
"esnext.array.d.ts",
217218
"esnext.promise.d.ts"
218219
];
219220

src/compiler/commandLineParser.ts

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ namespace ts {
144144
"es2017.string": "lib.es2017.string.d.ts",
145145
"es2017.intl": "lib.es2017.intl.d.ts",
146146
"es2017.typedarrays": "lib.es2017.typedarrays.d.ts",
147+
"esnext.array": "lib.esnext.array.d.ts",
147148
"esnext.asynciterable": "lib.esnext.asynciterable.d.ts",
148149
"esnext.promise": "lib.esnext.promise.d.ts",
149150
}),

src/harness/unittests/commandLineParsing.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ namespace ts {
6060
assertParseResult(["--lib", "es5,invalidOption", "0.ts"],
6161
{
6262
errors: [{
63-
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'es2018', 'esnext', 'dom', 'dom.iterable', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object', 'es2017.sharedmemory', 'es2017.string', 'es2017.intl', 'es2017.typedarrays', 'esnext.asynciterable', 'esnext.promise'.",
63+
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'es2018', 'esnext', 'dom', 'dom.iterable', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object', 'es2017.sharedmemory', 'es2017.string', 'es2017.intl', 'es2017.typedarrays', 'esnext.array', 'esnext.asynciterable', 'esnext.promise'.",
6464
category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category,
6565
code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code,
6666

@@ -263,7 +263,7 @@ namespace ts {
263263
assertParseResult(["--lib", "es5,", "es7", "0.ts"],
264264
{
265265
errors: [{
266-
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'es2018', 'esnext', 'dom', 'dom.iterable', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object', 'es2017.sharedmemory', 'es2017.string', 'es2017.intl', 'es2017.typedarrays', 'esnext.asynciterable', 'esnext.promise'.",
266+
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'es2018', 'esnext', 'dom', 'dom.iterable', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object', 'es2017.sharedmemory', 'es2017.string', 'es2017.intl', 'es2017.typedarrays', 'esnext.array', 'esnext.asynciterable', 'esnext.promise'.",
267267
category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category,
268268
code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code,
269269

@@ -283,7 +283,7 @@ namespace ts {
283283
assertParseResult(["--lib", "es5, ", "es7", "0.ts"],
284284
{
285285
errors: [{
286-
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'es2018', 'esnext', 'dom', 'dom.iterable', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object', 'es2017.sharedmemory', 'es2017.string', 'es2017.intl', 'es2017.typedarrays', 'esnext.asynciterable', 'esnext.promise'.",
286+
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'es2018', 'esnext', 'dom', 'dom.iterable', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object', 'es2017.sharedmemory', 'es2017.string', 'es2017.intl', 'es2017.typedarrays', 'esnext.array', 'esnext.asynciterable', 'esnext.promise'.",
287287
category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category,
288288
code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code,
289289

src/harness/unittests/convertCompilerOptionsFromJson.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ namespace ts {
266266
file: undefined,
267267
start: 0,
268268
length: 0,
269-
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'es2018', 'esnext', 'dom', 'dom.iterable', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object', 'es2017.sharedmemory', 'es2017.string', 'es2017.intl', 'es2017.typedarrays', 'esnext.asynciterable', 'esnext.promise'.",
269+
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'es2018', 'esnext', 'dom', 'dom.iterable', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object', 'es2017.sharedmemory', 'es2017.string', 'es2017.intl', 'es2017.typedarrays', 'esnext.array', 'esnext.asynciterable', 'esnext.promise'.",
270270
code: Diagnostics.Argument_for_0_option_must_be_Colon_1.code,
271271
category: Diagnostics.Argument_for_0_option_must_be_Colon_1.category
272272
}]
@@ -297,7 +297,7 @@ namespace ts {
297297
file: undefined,
298298
start: 0,
299299
length: 0,
300-
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'es2018', 'esnext', 'dom', 'dom.iterable', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object', 'es2017.sharedmemory', 'es2017.string', 'es2017.intl', 'es2017.typedarrays', 'esnext.asynciterable', 'esnext.promise'.",
300+
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'es2018', 'esnext', 'dom', 'dom.iterable', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object', 'es2017.sharedmemory', 'es2017.string', 'es2017.intl', 'es2017.typedarrays', 'esnext.array', 'esnext.asynciterable', 'esnext.promise'.",
301301
code: Diagnostics.Argument_for_0_option_must_be_Colon_1.code,
302302
category: Diagnostics.Argument_for_0_option_must_be_Colon_1.category
303303
}]
@@ -328,7 +328,7 @@ namespace ts {
328328
file: undefined,
329329
start: 0,
330330
length: 0,
331-
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'es2018', 'esnext', 'dom', 'dom.iterable', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object', 'es2017.sharedmemory', 'es2017.string', 'es2017.intl', 'es2017.typedarrays', 'esnext.asynciterable', 'esnext.promise'.",
331+
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'es2018', 'esnext', 'dom', 'dom.iterable', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object', 'es2017.sharedmemory', 'es2017.string', 'es2017.intl', 'es2017.typedarrays', 'esnext.array', 'esnext.asynciterable', 'esnext.promise'.",
332332
code: Diagnostics.Argument_for_0_option_must_be_Colon_1.code,
333333
category: Diagnostics.Argument_for_0_option_must_be_Colon_1.category
334334
}]
@@ -359,7 +359,7 @@ namespace ts {
359359
file: undefined,
360360
start: 0,
361361
length: 0,
362-
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'es2018', 'esnext', 'dom', 'dom.iterable', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object', 'es2017.sharedmemory', 'es2017.string', 'es2017.intl', 'es2017.typedarrays', 'esnext.asynciterable', 'esnext.promise'.",
362+
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'es2018', 'esnext', 'dom', 'dom.iterable', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object', 'es2017.sharedmemory', 'es2017.string', 'es2017.intl', 'es2017.typedarrays', 'esnext.array', 'esnext.asynciterable', 'esnext.promise'.",
363363
code: Diagnostics.Argument_for_0_option_must_be_Colon_1.code,
364364
category: Diagnostics.Argument_for_0_option_must_be_Colon_1.category
365365
}]

src/lib/es2018.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
/// <reference path="lib.es2017.d.ts" />
1+
/// <reference path="lib.es2017.d.ts" />

src/lib/esnext.array.d.ts

+203
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
interface ReadonlyArray<T> {
2+
3+
/**
4+
* Calls a defined callback function on each element of an array. Then, flattens the result into
5+
* a new array.
6+
* This is identical to a map followed by a flatten of depth 1.
7+
*
8+
* @param callback A function that accepts up to three arguments. The flatMap method calls the
9+
* callback function one time for each element in the array.
10+
* @param thisArg An object to which the this keyword can refer in the callback function. If
11+
* thisArg is omitted, undefined is used as the this value.
12+
*/
13+
flatMap<U, This = undefined> (
14+
callback: (this: This, value: T, index: number, array: T[]) => U|U[],
15+
thisArg?: This
16+
): U[]
17+
18+
19+
/**
20+
* Returns a new array with all sub-array elements concatenated into it recursively up to the
21+
* specified depth.
22+
*
23+
* @param depth The maximum recursion depth
24+
*/
25+
flatten<U>(this:
26+
ReadonlyArray<U[][][][]> |
27+
28+
ReadonlyArray<ReadonlyArray<U[][][]>> |
29+
ReadonlyArray<ReadonlyArray<U[][]>[]> |
30+
ReadonlyArray<ReadonlyArray<U[]>[][]> |
31+
ReadonlyArray<ReadonlyArray<U>[][][]> |
32+
33+
ReadonlyArray<ReadonlyArray<ReadonlyArray<U[][]>>> |
34+
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>[][]>> |
35+
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>[][]> |
36+
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>[]>[]> |
37+
ReadonlyArray<ReadonlyArray<ReadonlyArray<U[]>>[]> |
38+
ReadonlyArray<ReadonlyArray<ReadonlyArray<U[]>[]>> |
39+
40+
ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U[]>>>> |
41+
ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>[]>>> |
42+
ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>[]>> |
43+
ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>[]> |
44+
45+
ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>>>,
46+
depth: 4): U[];
47+
48+
/**
49+
* Returns a new array with all sub-array elements concatenated into it recursively up to the
50+
* specified depth.
51+
*
52+
* @param depth The maximum recursion depth
53+
*/
54+
flatten<U>(this:
55+
ReadonlyArray<U[][][]> |
56+
57+
ReadonlyArray<ReadonlyArray<U>[][]> |
58+
ReadonlyArray<ReadonlyArray<U[]>[]> |
59+
ReadonlyArray<ReadonlyArray<U[][]>> |
60+
61+
ReadonlyArray<ReadonlyArray<ReadonlyArray<U[]>>> |
62+
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>[]>> |
63+
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>[]> |
64+
65+
ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>>,
66+
depth: 3): U[];
67+
68+
/**
69+
* Returns a new array with all sub-array elements concatenated into it recursively up to the
70+
* specified depth.
71+
*
72+
* @param depth The maximum recursion depth
73+
*/
74+
flatten<U>(this:
75+
ReadonlyArray<U[][]> |
76+
77+
ReadonlyArray<ReadonlyArray<U[]>> |
78+
ReadonlyArray<ReadonlyArray<U>[]> |
79+
80+
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>,
81+
depth: 2): U[];
82+
83+
/**
84+
* Returns a new array with all sub-array elements concatenated into it recursively up to the
85+
* specified depth.
86+
*
87+
* @param depth The maximum recursion depth
88+
*/
89+
flatten<U>(this:
90+
ReadonlyArray<U[]> |
91+
ReadonlyArray<ReadonlyArray<U>>,
92+
depth?: 1
93+
): U[];
94+
95+
/**
96+
* Returns a new array with all sub-array elements concatenated into it recursively up to the
97+
* specified depth.
98+
*
99+
* @param depth The maximum recursion depth
100+
*/
101+
flatten<U>(this:
102+
ReadonlyArray<U>,
103+
depth: 0
104+
): U[];
105+
106+
/**
107+
* Returns a new array with all sub-array elements concatenated into it recursively up to the
108+
* specified depth. If no depth is provided, flatten method defaults to the depth of 1.
109+
*
110+
* @param depth The maximum recursion depth
111+
*/
112+
flatten<U>(depth?: number): any[];
113+
}
114+
115+
interface Array<T> {
116+
117+
/**
118+
* Calls a defined callback function on each element of an array. Then, flattens the result into
119+
* a new array.
120+
* This is identical to a map followed by a flatten of depth 1.
121+
*
122+
* @param callback A function that accepts up to three arguments. The flatMap method calls the
123+
* callback function one time for each element in the array.
124+
* @param thisArg An object to which the this keyword can refer in the callback function. If
125+
* thisArg is omitted, undefined is used as the this value.
126+
*/
127+
flatMap<U, This = undefined> (
128+
callback: (this: This, value: T, index: number, array: T[]) => U|U[],
129+
thisArg?: This
130+
): U[]
131+
132+
/**
133+
* Returns a new array with all sub-array elements concatenated into it recursively up to the
134+
* specified depth.
135+
*
136+
* @param depth The maximum recursion depth
137+
*/
138+
flatten<U>(this: U[][][][][][][][], depth: 7): U[];
139+
140+
/**
141+
* Returns a new array with all sub-array elements concatenated into it recursively up to the
142+
* specified depth.
143+
*
144+
* @param depth The maximum recursion depth
145+
*/
146+
flatten<U>(this: U[][][][][][][], depth: 6): U[];
147+
148+
/**
149+
* Returns a new array with all sub-array elements concatenated into it recursively up to the
150+
* specified depth.
151+
*
152+
* @param depth The maximum recursion depth
153+
*/
154+
flatten<U>(this: U[][][][][][], depth: 5): U[];
155+
156+
/**
157+
* Returns a new array with all sub-array elements concatenated into it recursively up to the
158+
* specified depth.
159+
*
160+
* @param depth The maximum recursion depth
161+
*/
162+
flatten<U>(this: U[][][][][], depth: 4): U[];
163+
164+
/**
165+
* Returns a new array with all sub-array elements concatenated into it recursively up to the
166+
* specified depth.
167+
*
168+
* @param depth The maximum recursion depth
169+
*/
170+
flatten<U>(this: U[][][][], depth: 3): U[];
171+
172+
/**
173+
* Returns a new array with all sub-array elements concatenated into it recursively up to the
174+
* specified depth.
175+
*
176+
* @param depth The maximum recursion depth
177+
*/
178+
flatten<U>(this: U[][][], depth: 2): U[];
179+
180+
/**
181+
* Returns a new array with all sub-array elements concatenated into it recursively up to the
182+
* specified depth.
183+
*
184+
* @param depth The maximum recursion depth
185+
*/
186+
flatten<U>(this: U[][], depth?: 1): U[];
187+
188+
/**
189+
* Returns a new array with all sub-array elements concatenated into it recursively up to the
190+
* specified depth.
191+
*
192+
* @param depth The maximum recursion depth
193+
*/
194+
flatten<U>(this: U[], depth: 0): U[];
195+
196+
/**
197+
* Returns a new array with all sub-array elements concatenated into it recursively up to the
198+
* specified depth. If no depth is provided, flatten method defaults to the depth of 1.
199+
*
200+
* @param depth The maximum recursion depth
201+
*/
202+
flatten<U>(depth?: number): any[];
203+
}

src/lib/esnext.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/// <reference path="lib.es2018.d.ts" />
22
/// <reference path="lib.esnext.asynciterable.d.ts" />
3+
/// <reference path="lib.esnext.array.d.ts" />
34
/// <reference path="lib.esnext.promise.d.ts" />

0 commit comments

Comments
 (0)