Skip to content

Commit c4022b9

Browse files
committed
Better typings for Array.concat()
1 parent 16ceef5 commit c4022b9

31 files changed

+124
-156
lines changed

src/lib/es5.d.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1080,12 +1080,12 @@ 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[];
1083+
concat(...items: (T | ConcatArray<T>)[]): T[];
10841084
/**
10851085
* Combines two or more arrays.
10861086
* @param items Additional items to add to the end of array1.
10871087
*/
1088-
concat(...items: (T | ConcatArray<T>)[]): T[];
1088+
concat<U>(...items: (U | ConcatArray<U>)[]): (T | U)[];
10891089
/**
10901090
* Adds all the elements of an array separated by the specified separator string.
10911091
* @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.
@@ -1214,12 +1214,12 @@ interface Array<T> {
12141214
* Combines two or more arrays.
12151215
* @param items Additional items to add to the end of array1.
12161216
*/
1217-
concat(...items: ConcatArray<T>[]): T[];
1217+
concat(...items: (T | ConcatArray<T>)[]): T[];
12181218
/**
12191219
* Combines two or more arrays.
12201220
* @param items Additional items to add to the end of array1.
12211221
*/
1222-
concat(...items: (T | ConcatArray<T>)[]): T[];
1222+
concat<U>(...items: (U | ConcatArray<U>)[]): (T | U)[];
12231223
/**
12241224
* Adds all the elements of an array separated by the specified separator string.
12251225
* @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: (string | ConcatArray<string>)[]): string[]; <U>(...items: (U | ConcatArray<U>)[]): (string | U)[]; }
99
>a : string[]
10-
>concat : { (...items: ConcatArray<string>[]): string[]; (...items: (string | ConcatArray<string>)[]): string[]; }
10+
>concat : { (...items: (string | ConcatArray<string>)[]): string[]; <U>(...items: (U | ConcatArray<U>)[]): (string | U)[]; }
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: (string | ConcatArray<string>)[]): string[]; <U>(...items: (U | ConcatArray<U>)[]): (string | U)[]; }
1717
>a : string[]
18-
>concat : { (...items: ConcatArray<string>[]): string[]; (...items: (string | ConcatArray<string>)[]): string[]; }
18+
>concat : { (...items: (string | ConcatArray<string>)[]): string[]; <U>(...items: (U | ConcatArray<U>)[]): (string | U)[]; }
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: (string | ConcatArray<string>)[]): string[]; <U>(...items: (U | ConcatArray<U>)[]): (string | U)[]; }
2929
>b : string[]
30-
>concat : { (...items: ConcatArray<string>[]): string[]; (...items: (string | ConcatArray<string>)[]): string[]; }
30+
>concat : { (...items: (string | ConcatArray<string>)[]): string[]; <U>(...items: (U | ConcatArray<U>)[]): (string | U)[]; }
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: (Fn<T1> | ConcatArray<Fn<T1>>)[]): Fn<T1>[]; <U>(...items: (U | ConcatArray<U>)[]): (Fn<T1> | U)[]; }
1515
>b : Fn<T1>[]
16-
>concat : { (...items: ConcatArray<Fn<T1>>[]): Fn<T1>[]; (...items: (Fn<T1> | ConcatArray<Fn<T1>>)[]): Fn<T1>[]; }
16+
>concat : { (...items: (Fn<T1> | ConcatArray<Fn<T1>>)[]): Fn<T1>[]; <U>(...items: (U | ConcatArray<U>)[]): (Fn<T1> | U)[]; }
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: any[]): any[]; <U>(...items: (U | ConcatArray<U>)[]): any[]; }
88
>[] : undefined[]
9-
>concat : { (...items: ConcatArray<any>[]): any[]; (...items: any[]): any[]; }
9+
>concat : { (...items: any[]): any[]; <U>(...items: (U | ConcatArray<U>)[]): any[]; }
1010
>[{ a: 1 }] : { a: number; }[]
1111
>{ a: 1 } : { a: number; }
1212
>a : number

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: (number | ConcatArray<number>)[]): number[]; <U>(...items: (U | ConcatArray<U>)[]): (number | U)[]; }
1919
>fa : number[]
20-
>concat : { (...items: ConcatArray<number>[]): number[]; (...items: (number | ConcatArray<number>)[]): number[]; }
20+
>concat : { (...items: (number | ConcatArray<number>)[]): number[]; <U>(...items: (U | ConcatArray<U>)[]): (number | U)[]; }
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: (number | ConcatArray<number>)[]): number[]; <U>(...items: (U | ConcatArray<U>)[]): (number | U)[]; }
2929
>fa : number[]
30-
>concat : { (...items: ConcatArray<number>[]): number[]; (...items: (number | ConcatArray<number>)[]): number[]; }
30+
>concat : { (...items: (number | ConcatArray<number>)[]): number[]; <U>(...items: (U | ConcatArray<U>)[]): (number | U)[]; }
3131
>0 : 0
3232

3333

tests/baselines/reference/concatTuples.types

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ 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: ([number, number] | ConcatArray<[number, number]>)[]): [number, number][]; <U>(...items: (U | ConcatArray<U>)[]): ([number, number] | U)[]; }
1414
>ijs : [number, number][]
15-
>concat : { (...items: ConcatArray<[number, number]>[]): [number, number][]; (...items: ([number, number] | ConcatArray<[number, number]>)[]): [number, number][]; }
16-
>[[3, 4], [5, 6]] : [number, number][]
15+
>concat : { (...items: ([number, number] | ConcatArray<[number, number]>)[]): [number, number][]; <U>(...items: (U | ConcatArray<U>)[]): ([number, number] | U)[]; }
16+
>[[3, 4], [5, 6]] : [[number, number], [number, number]]
1717
>[3, 4] : [number, number]
1818
>3 : 3
1919
>4 : 4

tests/baselines/reference/contextualExpressionTypecheckingDoesntBlowStack.types

+4-4
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ export default class Operation {
6969

7070
// Commenting out this line will fix the problem.
7171
result = (result || []).concat(innerResult);
72-
>result = (result || []).concat(innerResult) : IValidationError[]
72+
>result = (result || []).concat(innerResult) : any[]
7373
>result : IValidationError[] | null
74-
>(result || []).concat(innerResult) : IValidationError[]
75-
>(result || []).concat : { (...items: ConcatArray<IValidationError>[]): IValidationError[]; (...items: (IValidationError | ConcatArray<IValidationError>)[]): IValidationError[]; }
74+
>(result || []).concat(innerResult) : any[]
75+
>(result || []).concat : { (...items: (IValidationError | ConcatArray<IValidationError>)[]): IValidationError[]; <U>(...items: (U | ConcatArray<U>)[]): (IValidationError | U)[]; }
7676
>(result || []) : IValidationError[]
7777
>result || [] : IValidationError[]
7878
>result : IValidationError[] | null
7979
>[] : never[]
80-
>concat : { (...items: ConcatArray<IValidationError>[]): IValidationError[]; (...items: (IValidationError | ConcatArray<IValidationError>)[]): IValidationError[]; }
80+
>concat : { (...items: (IValidationError | ConcatArray<IValidationError>)[]): IValidationError[]; <U>(...items: (U | ConcatArray<U>)[]): (IValidationError | U)[]; }
8181
>innerResult : any
8282
}
8383
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
tests/cases/compiler/destructuringTuple.ts(11,8): error TS2493: Tuple type '[]' of length '0' has no element at index '0'.
2-
tests/cases/compiler/destructuringTuple.ts(11,60): error TS2769: No overload matches this call.
3-
Overload 1 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
4-
Argument of type 'number' is not assignable to parameter of type 'ConcatArray<never>'.
5-
Overload 2 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
6-
Argument of type 'number' is not assignable to parameter of type 'ConcatArray<never>'.
1+
tests/cases/compiler/destructuringTuple.ts(11,48): error TS2769: No overload matches this call.
2+
Overload 1 of 3, '(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number', gave the following error.
3+
Type 'number[]' is not assignable to type 'number'.
4+
Overload 2 of 3, '(callbackfn: (previousValue: [], currentValue: number, currentIndex: number, array: number[]) => [], initialValue: []): []', gave the following error.
5+
Type 'number[]' is not assignable to type '[]'.
6+
Types of property 'length' are incompatible.
7+
Type 'number' is not assignable to type '0'.
78

89

9-
==== tests/cases/compiler/destructuringTuple.ts (2 errors) ====
10+
==== tests/cases/compiler/destructuringTuple.ts (1 errors) ====
1011
declare var tuple: [boolean, number, ...string[]];
1112

1213
const [a, b, c, ...rest] = tuple;
@@ -18,14 +19,16 @@ tests/cases/compiler/destructuringTuple.ts(11,60): error TS2769: No overload mat
1819
// Repros from #32140
1920

2021
const [oops1] = [1, 2, 3].reduce((accu, el) => accu.concat(el), []);
21-
~~~~~
22-
!!! error TS2493: Tuple type '[]' of length '0' has no element at index '0'.
23-
~~
22+
~~~~~~~~~~~~~~~
2423
!!! error TS2769: No overload matches this call.
25-
!!! error TS2769: Overload 1 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
26-
!!! error TS2769: Argument of type 'number' is not assignable to parameter of type 'ConcatArray<never>'.
27-
!!! error TS2769: Overload 2 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
28-
!!! error TS2769: Argument of type 'number' is not assignable to parameter of type 'ConcatArray<never>'.
24+
!!! error TS2769: Overload 1 of 3, '(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number', gave the following error.
25+
!!! error TS2769: Type 'number[]' is not assignable to type 'number'.
26+
!!! error TS2769: Overload 2 of 3, '(callbackfn: (previousValue: [], currentValue: number, currentIndex: number, array: number[]) => [], initialValue: []): []', gave the following error.
27+
!!! error TS2769: Type 'number[]' is not assignable to type '[]'.
28+
!!! error TS2769: Types of property 'length' are incompatible.
29+
!!! error TS2769: Type 'number' is not assignable to type '0'.
30+
!!! related TS6502 /.ts/lib.es5.d.ts:1350:24: The expected type comes from the return type of this signature.
31+
!!! related TS6502 /.ts/lib.es5.d.ts:1356:27: The expected type comes from the return type of this signature.
2932

3033
const [oops2] = [1, 2, 3].reduce((acc: number[], e) => acc.concat(e), []);
3134

tests/baselines/reference/destructuringTuple.types

+9-9
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ declare var receiver: typeof tuple;
2323
// Repros from #32140
2424

2525
const [oops1] = [1, 2, 3].reduce((accu, el) => accu.concat(el), []);
26-
>oops1 : undefined
27-
>[1, 2, 3].reduce((accu, el) => accu.concat(el), []) : []
26+
>oops1 : any
27+
>[1, 2, 3].reduce((accu, el) => accu.concat(el), []) : any
2828
>[1, 2, 3].reduce : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number): number; (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number; <U>(callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; }
2929
>[1, 2, 3] : number[]
3030
>1 : 1
3131
>2 : 2
3232
>3 : 3
3333
>reduce : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number): number; (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number; <U>(callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; }
34-
>(accu, el) => accu.concat(el) : (accu: [], el: number) => any
34+
>(accu, el) => accu.concat(el) : (accu: [], el: number) => number[]
3535
>accu : []
3636
>el : number
37-
>accu.concat(el) : any
38-
>accu.concat : { (...items: ConcatArray<never>[]): never[]; (...items: ConcatArray<never>[]): never[]; }
37+
>accu.concat(el) : number[]
38+
>accu.concat : { (...items: ConcatArray<never>[]): never[]; <U>(...items: (U | ConcatArray<U>)[]): U[]; }
3939
>accu : []
40-
>concat : { (...items: ConcatArray<never>[]): never[]; (...items: ConcatArray<never>[]): never[]; }
40+
>concat : { (...items: ConcatArray<never>[]): never[]; <U>(...items: (U | ConcatArray<U>)[]): U[]; }
4141
>el : number
42-
>[] : []
42+
>[] : never[]
4343

4444
const [oops2] = [1, 2, 3].reduce((acc: number[], e) => acc.concat(e), []);
4545
>oops2 : number
@@ -54,9 +54,9 @@ const [oops2] = [1, 2, 3].reduce((acc: number[], e) => acc.concat(e), []);
5454
>acc : number[]
5555
>e : number
5656
>acc.concat(e) : number[]
57-
>acc.concat : { (...items: ConcatArray<number>[]): number[]; (...items: (number | ConcatArray<number>)[]): number[]; }
57+
>acc.concat : { (...items: (number | ConcatArray<number>)[]): number[]; <U>(...items: (U | ConcatArray<U>)[]): (number | U)[]; }
5858
>acc : number[]
59-
>concat : { (...items: ConcatArray<number>[]): number[]; (...items: (number | ConcatArray<number>)[]): number[]; }
59+
>concat : { (...items: (number | ConcatArray<number>)[]): number[]; <U>(...items: (U | ConcatArray<U>)[]): (number | U)[]; }
6060
>e : number
6161
>[] : never[]
6262

tests/baselines/reference/emitSkipsThisWithRestParameter.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ function rebase(fn: (base: any, ...args: any[]) => any): (...args: any[]) => any
1818
>apply : (this: Function, thisArg: any, argArray?: any) => any
1919
>this : any
2020
>[ this ].concat(args) : any[]
21-
>[ this ].concat : { (...items: ConcatArray<any>[]): any[]; (...items: any[]): any[]; }
21+
>[ this ].concat : { (...items: any[]): any[]; <U>(...items: (U | ConcatArray<U>)[]): any[]; }
2222
>[ this ] : any[]
2323
>this : any
24-
>concat : { (...items: ConcatArray<any>[]): any[]; (...items: any[]): any[]; }
24+
>concat : { (...items: any[]): any[]; <U>(...items: (U | ConcatArray<U>)[]): any[]; }
2525
>args : any[]
2626

2727
};

tests/baselines/reference/intersectionTypeInference3.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ declare const b: Set<A>;
2424
const c1 = Array.from(a).concat(Array.from(b));
2525
>c1 : Nominal<"A", string>[]
2626
>Array.from(a).concat(Array.from(b)) : Nominal<"A", string>[]
27-
>Array.from(a).concat : { (...items: ConcatArray<Nominal<"A", string>>[]): Nominal<"A", string>[]; (...items: (Nominal<"A", string> | ConcatArray<Nominal<"A", string>>)[]): Nominal<"A", string>[]; }
27+
>Array.from(a).concat : { (...items: (Nominal<"A", string> | ConcatArray<Nominal<"A", string>>)[]): Nominal<"A", string>[]; <U>(...items: (U | ConcatArray<U>)[]): (Nominal<"A", string> | U)[]; }
2828
>Array.from(a) : Nominal<"A", string>[]
2929
>Array.from : { <T>(arrayLike: ArrayLike<T>): T[]; <T, U>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => U, thisArg?: any): U[]; <T>(iterable: Iterable<T> | ArrayLike<T>): T[]; <T, U>(iterable: Iterable<T> | ArrayLike<T>, mapfn: (v: T, k: number) => U, thisArg?: any): U[]; }
3030
>Array : ArrayConstructor
3131
>from : { <T>(arrayLike: ArrayLike<T>): T[]; <T, U>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => U, thisArg?: any): U[]; <T>(iterable: Iterable<T> | ArrayLike<T>): T[]; <T, U>(iterable: Iterable<T> | ArrayLike<T>, mapfn: (v: T, k: number) => U, thisArg?: any): U[]; }
3232
>a : Set<Nominal<"A", string>>
33-
>concat : { (...items: ConcatArray<Nominal<"A", string>>[]): Nominal<"A", string>[]; (...items: (Nominal<"A", string> | ConcatArray<Nominal<"A", string>>)[]): Nominal<"A", string>[]; }
33+
>concat : { (...items: (Nominal<"A", string> | ConcatArray<Nominal<"A", string>>)[]): Nominal<"A", string>[]; <U>(...items: (U | ConcatArray<U>)[]): (Nominal<"A", string> | U)[]; }
3434
>Array.from(b) : Nominal<"A", string>[]
3535
>Array.from : { <T>(arrayLike: ArrayLike<T>): T[]; <T, U>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => U, thisArg?: any): U[]; <T>(iterable: Iterable<T> | ArrayLike<T>): T[]; <T, U>(iterable: Iterable<T> | ArrayLike<T>, mapfn: (v: T, k: number) => U, thisArg?: any): U[]; }
3636
>Array : ArrayConstructor

0 commit comments

Comments
 (0)