Skip to content

Commit bdd2ac3

Browse files
committed
Better typings for Array.concat(), etc.
1 parent eb0208c commit bdd2ac3

38 files changed

+143
-314
lines changed

src/harness/fourslash.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4687,6 +4687,7 @@ namespace FourSlashInterface {
46874687
varEntry("JSON"),
46884688
interfaceEntry("ReadonlyArray"),
46894689
interfaceEntry("ConcatArray"),
4690+
typeEntry("ConcatFlatten"),
46904691
varEntry("Array"),
46914692
interfaceEntry("ArrayConstructor"),
46924693
interfaceEntry("TypedPropertyDescriptor"),

src/lib/es2019.array.d.ts

+22-162
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
type Flatten<T> = T extends readonly (infer U)[] ? U : T;
2+
13
interface ReadonlyArray<T> {
24

35
/**
@@ -10,106 +12,23 @@ interface ReadonlyArray<T> {
1012
* @param thisArg An object to which the this keyword can refer in the callback function. If
1113
* thisArg is omitted, undefined is used as the this value.
1214
*/
13-
flatMap<U, This = undefined> (
14-
callback: (this: This, value: T, index: number, array: T[]) => U | ReadonlyArray<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-
flat<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-
flat<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-
flat<U>(this:
75-
ReadonlyArray<U[][]> |
76-
77-
ReadonlyArray<ReadonlyArray<U[]>> |
78-
ReadonlyArray<ReadonlyArray<U>[]> |
79-
80-
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>,
81-
depth: 2): U[];
15+
flatMap<U> (
16+
callbackfn: (value: T, index: number, array: readonly T[]) => U,
17+
thisArg?: any
18+
): Flatten<U>[];
8219

8320
/**
8421
* Returns a new array with all sub-array elements concatenated into it recursively up to the
8522
* specified depth.
8623
*
8724
* @param depth The maximum recursion depth
8825
*/
89-
flat<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-
flat<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, flat method defaults to the depth of 1.
109-
*
110-
* @param depth The maximum recursion depth
111-
*/
112-
flat<U>(depth?: number): any[];
26+
flat(depth: 4): Flatten<Flatten<Flatten<Flatten<this[number]>>>>[];
27+
flat(depth: 3): Flatten<Flatten<Flatten<this[number]>>>[];
28+
flat(depth: 2): Flatten<Flatten<this[number]>>[];
29+
flat(depth?: 1): Flatten<this[number]>[];
30+
flat(depth: 0): { -readonly [P in keyof this]: this[P] };
31+
flat(depth: number): any[];
11332
}
11433

11534
interface Array<T> {
@@ -124,80 +43,21 @@ interface Array<T> {
12443
* @param thisArg An object to which the this keyword can refer in the callback function. If
12544
* thisArg is omitted, undefined is used as the this value.
12645
*/
127-
flatMap<U, This = undefined> (
128-
callback: (this: This, value: T, index: number, array: T[]) => U | ReadonlyArray<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-
flat<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-
flat<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-
flat<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-
flat<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-
flat<U>(this: U[][][][], depth: 3): U[];
46+
flatMap<U> (
47+
callbackfn: (value: T, index: number, array: T[]) => U,
48+
thisArg?: any
49+
): Flatten<U>[];
17150

17251
/**
17352
* Returns a new array with all sub-array elements concatenated into it recursively up to the
17453
* specified depth.
17554
*
17655
* @param depth The maximum recursion depth
17756
*/
178-
flat<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-
flat<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-
flat<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, flat method defaults to the depth of 1.
199-
*
200-
* @param depth The maximum recursion depth
201-
*/
202-
flat<U>(depth?: number): any[];
57+
flat(depth: 4): Flatten<Flatten<Flatten<Flatten<this[number]>>>>[];
58+
flat(depth: 3): Flatten<Flatten<Flatten<this[number]>>>[];
59+
flat(depth: 2): Flatten<Flatten<this[number]>>[];
60+
flat(depth?: 1): Flatten<this[number]>[];
61+
flat(depth: 0): { -readonly [P in keyof this]: this[P] };
62+
flat(depth: number): any[];
20363
}

src/lib/es5.d.ts

+5-12
Original file line numberDiff line numberDiff line change
@@ -1080,12 +1080,8 @@ interface ReadonlyArray<T> {
10801080
* Combines two or more arrays.
10811081
* @param items Additional items to add to the end of array1.
10821082
*/
1083-
concat(...items: ConcatArray<T>[]): T[];
1084-
/**
1085-
* Combines two or more arrays.
1086-
* @param items Additional items to add to the end of array1.
1087-
*/
1088-
concat(...items: (T | ConcatArray<T>)[]): T[];
1083+
concat(...items: ConcatArray<this[number]>[]): this[number][];
1084+
concat<U extends any[]>(...items: U): (this[number] | ConcatFlatten<U[number]>)[];
10891085
/**
10901086
* Adds all the elements of an array separated by the specified separator string.
10911087
* @param separator A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.
@@ -1187,6 +1183,7 @@ interface ConcatArray<T> {
11871183
join(separator?: string): string;
11881184
slice(start?: number, end?: number): T[];
11891185
}
1186+
type ConcatFlatten<T> = T extends ConcatArray<infer U> ? U : T;
11901187

11911188
interface Array<T> {
11921189
/**
@@ -1214,12 +1211,8 @@ interface Array<T> {
12141211
* Combines two or more arrays.
12151212
* @param items Additional items to add to the end of array1.
12161213
*/
1217-
concat(...items: ConcatArray<T>[]): T[];
1218-
/**
1219-
* Combines two or more arrays.
1220-
* @param items Additional items to add to the end of array1.
1221-
*/
1222-
concat(...items: (T | ConcatArray<T>)[]): T[];
1214+
concat(...items: ConcatArray<this[number]>[]): this[number][];
1215+
concat<U extends any[]>(...items: U): (this[number] | ConcatFlatten<U[number]>)[];
12231216
/**
12241217
* Adds all the elements of an array separated by the specified separator string.
12251218
* @param separator A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.

tests/baselines/reference/arrayConcat2.types

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ var a: string[] = [];
55

66
a.concat("hello", 'world');
77
>a.concat("hello", 'world') : string[]
8-
>a.concat : { (...items: ConcatArray<string>[]): string[]; (...items: (string | ConcatArray<string>)[]): string[]; }
8+
>a.concat : { (...items: ConcatArray<string>[]): string[]; <U extends any[]>(...items: U): (string | ConcatFlatten<U[number]>)[]; }
99
>a : string[]
10-
>concat : { (...items: ConcatArray<string>[]): string[]; (...items: (string | ConcatArray<string>)[]): string[]; }
10+
>concat : { (...items: ConcatArray<string>[]): string[]; <U extends any[]>(...items: U): (string | ConcatFlatten<U[number]>)[]; }
1111
>"hello" : "hello"
1212
>'world' : "world"
1313

1414
a.concat('Hello');
1515
>a.concat('Hello') : string[]
16-
>a.concat : { (...items: ConcatArray<string>[]): string[]; (...items: (string | ConcatArray<string>)[]): string[]; }
16+
>a.concat : { (...items: ConcatArray<string>[]): string[]; <U extends any[]>(...items: U): (string | ConcatFlatten<U[number]>)[]; }
1717
>a : string[]
18-
>concat : { (...items: ConcatArray<string>[]): string[]; (...items: (string | ConcatArray<string>)[]): string[]; }
18+
>concat : { (...items: ConcatArray<string>[]): string[]; <U extends any[]>(...items: U): (string | ConcatFlatten<U[number]>)[]; }
1919
>'Hello' : "Hello"
2020

2121
var b = new Array<string>();
@@ -25,8 +25,8 @@ var b = new Array<string>();
2525

2626
b.concat('hello');
2727
>b.concat('hello') : string[]
28-
>b.concat : { (...items: ConcatArray<string>[]): string[]; (...items: (string | ConcatArray<string>)[]): string[]; }
28+
>b.concat : { (...items: ConcatArray<string>[]): string[]; <U extends any[]>(...items: U): (string | ConcatFlatten<U[number]>)[]; }
2929
>b : string[]
30-
>concat : { (...items: ConcatArray<string>[]): string[]; (...items: (string | ConcatArray<string>)[]): string[]; }
30+
>concat : { (...items: ConcatArray<string>[]): string[]; <U extends any[]>(...items: U): (string | ConcatFlatten<U[number]>)[]; }
3131
>'hello' : "hello"
3232

tests/baselines/reference/arrayConcat3.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ function doStuff<T extends object, T1 extends T>(a: Array<Fn<T>>, b: Array<Fn<T1
1111

1212
b.concat(a);
1313
>b.concat(a) : Fn<T1>[]
14-
>b.concat : { (...items: ConcatArray<Fn<T1>>[]): Fn<T1>[]; (...items: (Fn<T1> | ConcatArray<Fn<T1>>)[]): Fn<T1>[]; }
14+
>b.concat : { (...items: ConcatArray<Fn<T1>>[]): Fn<T1>[]; <U extends any[]>(...items: U): (Fn<T1> | ConcatFlatten<U[number]>)[]; }
1515
>b : Fn<T1>[]
16-
>concat : { (...items: ConcatArray<Fn<T1>>[]): Fn<T1>[]; (...items: (Fn<T1> | ConcatArray<Fn<T1>>)[]): Fn<T1>[]; }
16+
>concat : { (...items: ConcatArray<Fn<T1>>[]): Fn<T1>[]; <U extends any[]>(...items: U): (Fn<T1> | ConcatFlatten<U[number]>)[]; }
1717
>a : Fn<T>[]
1818
}
1919

tests/baselines/reference/arrayConcatMap.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ var x = [].concat([{ a: 1 }], [{ a: 2 }])
44
>[].concat([{ a: 1 }], [{ a: 2 }]) .map(b => b.a) : any[]
55
>[].concat([{ a: 1 }], [{ a: 2 }]) .map : <U>(callbackfn: (value: any, index: number, array: any[]) => U, thisArg?: any) => U[]
66
>[].concat([{ a: 1 }], [{ a: 2 }]) : any[]
7-
>[].concat : { (...items: ConcatArray<any>[]): any[]; (...items: any[]): any[]; }
7+
>[].concat : { (...items: ConcatArray<any>[]): any[]; <U extends any[]>(...items: U): any[]; }
88
>[] : undefined[]
9-
>concat : { (...items: ConcatArray<any>[]): any[]; (...items: any[]): any[]; }
9+
>concat : { (...items: ConcatArray<any>[]): any[]; <U extends any[]>(...items: U): any[]; }
1010
>[{ a: 1 }] : { a: number; }[]
1111
>{ a: 1 } : { a: number; }
1212
>a : number

tests/baselines/reference/arrayFlatMap.types

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ const readonlyArray: ReadonlyArray<number> = [];
99

1010
array.flatMap((): ReadonlyArray<number> => []); // ok
1111
>array.flatMap((): ReadonlyArray<number> => []) : number[]
12-
>array.flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U | readonly U[], thisArg?: This) => U[]
12+
>array.flatMap : <U>(callbackfn: (value: number, index: number, array: number[]) => U, thisArg?: any) => Flatten<U>[]
1313
>array : number[]
14-
>flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U | readonly U[], thisArg?: This) => U[]
14+
>flatMap : <U>(callbackfn: (value: number, index: number, array: number[]) => U, thisArg?: any) => Flatten<U>[]
1515
>(): ReadonlyArray<number> => [] : () => readonly number[]
1616
>[] : undefined[]
1717

1818
readonlyArray.flatMap((): ReadonlyArray<number> => []); // ok
1919
>readonlyArray.flatMap((): ReadonlyArray<number> => []) : number[]
20-
>readonlyArray.flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U | readonly U[], thisArg?: This) => U[]
20+
>readonlyArray.flatMap : <U>(callbackfn: (value: number, index: number, array: readonly number[]) => U, thisArg?: any) => Flatten<U>[]
2121
>readonlyArray : readonly number[]
22-
>flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U | readonly U[], thisArg?: This) => U[]
22+
>flatMap : <U>(callbackfn: (value: number, index: number, array: readonly number[]) => U, thisArg?: any) => Flatten<U>[]
2323
>(): ReadonlyArray<number> => [] : () => readonly number[]
2424
>[] : undefined[]
2525

tests/baselines/reference/arrayOfSubtypeIsAssignableToReadonlyArray.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts(13,1): error TS2322: Type 'A[]' is not assignable to type 'readonly B[]'.
22
Property 'b' is missing in type 'A' but required in type 'B'.
33
tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts(18,1): error TS2322: Type 'C<A>' is not assignable to type 'readonly B[]'.
4-
The types returned by 'concat(...)' are incompatible between these types.
4+
The types returned by 'slice(...)' are incompatible between these types.
55
Type 'A[]' is not assignable to type 'B[]'.
66
Type 'A' is not assignable to type 'B'.
77

@@ -31,7 +31,7 @@ tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts(18,1): error T
3131
rrb = cra; // error: 'A' is not assignable to 'B'
3232
~~~
3333
!!! error TS2322: Type 'C<A>' is not assignable to type 'readonly B[]'.
34-
!!! error TS2322: The types returned by 'concat(...)' are incompatible between these types.
34+
!!! error TS2322: The types returned by 'slice(...)' are incompatible between these types.
3535
!!! error TS2322: Type 'A[]' is not assignable to type 'B[]'.
3636
!!! error TS2322: Type 'A' is not assignable to type 'B'.
3737

tests/baselines/reference/concatError.types

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ fa = fa.concat([0]);
1515
>fa = fa.concat([0]) : number[]
1616
>fa : number[]
1717
>fa.concat([0]) : number[]
18-
>fa.concat : { (...items: ConcatArray<number>[]): number[]; (...items: (number | ConcatArray<number>)[]): number[]; }
18+
>fa.concat : { (...items: ConcatArray<number>[]): number[]; <U extends any[]>(...items: U): (number | ConcatFlatten<U[number]>)[]; }
1919
>fa : number[]
20-
>concat : { (...items: ConcatArray<number>[]): number[]; (...items: (number | ConcatArray<number>)[]): number[]; }
20+
>concat : { (...items: ConcatArray<number>[]): number[]; <U extends any[]>(...items: U): (number | ConcatFlatten<U[number]>)[]; }
2121
>[0] : number[]
2222
>0 : 0
2323

2424
fa = fa.concat(0);
2525
>fa = fa.concat(0) : number[]
2626
>fa : number[]
2727
>fa.concat(0) : number[]
28-
>fa.concat : { (...items: ConcatArray<number>[]): number[]; (...items: (number | ConcatArray<number>)[]): number[]; }
28+
>fa.concat : { (...items: ConcatArray<number>[]): number[]; <U extends any[]>(...items: U): (number | ConcatFlatten<U[number]>)[]; }
2929
>fa : number[]
30-
>concat : { (...items: ConcatArray<number>[]): number[]; (...items: (number | ConcatArray<number>)[]): number[]; }
30+
>concat : { (...items: ConcatArray<number>[]): number[]; <U extends any[]>(...items: U): (number | ConcatFlatten<U[number]>)[]; }
3131
>0 : 0
3232

3333

tests/baselines/reference/concatTuples.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ ijs = ijs.concat([[3, 4], [5, 6]]);
1010
>ijs = ijs.concat([[3, 4], [5, 6]]) : [number, number][]
1111
>ijs : [number, number][]
1212
>ijs.concat([[3, 4], [5, 6]]) : [number, number][]
13-
>ijs.concat : { (...items: ConcatArray<[number, number]>[]): [number, number][]; (...items: ([number, number] | ConcatArray<[number, number]>)[]): [number, number][]; }
13+
>ijs.concat : { (...items: ConcatArray<[number, number]>[]): [number, number][]; <U extends any[]>(...items: U): ([number, number] | ConcatFlatten<U[number]>)[]; }
1414
>ijs : [number, number][]
15-
>concat : { (...items: ConcatArray<[number, number]>[]): [number, number][]; (...items: ([number, number] | ConcatArray<[number, number]>)[]): [number, number][]; }
15+
>concat : { (...items: ConcatArray<[number, number]>[]): [number, number][]; <U extends any[]>(...items: U): ([number, number] | ConcatFlatten<U[number]>)[]; }
1616
>[[3, 4], [5, 6]] : [number, number][]
1717
>[3, 4] : [number, number]
1818
>3 : 3

0 commit comments

Comments
 (0)