Skip to content

Commit 42ef827

Browse files
author
Andy Hanson
committed
Hack to allow concat to work even when an Array isn't assignable to ReadonlyArray
1 parent 8f23bf8 commit 42ef827

File tree

7 files changed

+95
-12
lines changed

7 files changed

+95
-12
lines changed

src/lib/es5.d.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -992,12 +992,13 @@ interface ReadonlyArray<T> {
992992
* Combines two or more arrays.
993993
* @param items Additional items to add to the end of array1.
994994
*/
995-
concat(...items: ReadonlyArray<T>[]): T[];
995+
// TODO: https://github.com/Microsoft/TypeScript/issues/20454
996+
concat(...items: (T[] | ReadonlyArray<T>)[]): T[];
996997
/**
997998
* Combines two or more arrays.
998999
* @param items Additional items to add to the end of array1.
9991000
*/
1000-
concat(...items: (T | ReadonlyArray<T>)[]): T[];
1001+
concat(...items: (T | T[] | ReadonlyArray<T>)[]): T[];
10011002
/**
10021003
* Adds all the elements of an array separated by the specified separator string.
10031004
* @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.
@@ -1113,12 +1114,12 @@ interface Array<T> {
11131114
* Combines two or more arrays.
11141115
* @param items Additional items to add to the end of array1.
11151116
*/
1116-
concat(...items: ReadonlyArray<T>[]): T[];
1117+
concat(...items: (T[] | ReadonlyArray<T>)[]): T[];
11171118
/**
11181119
* Combines two or more arrays.
11191120
* @param items Additional items to add to the end of array1.
11201121
*/
1121-
concat(...items: (T | ReadonlyArray<T>)[]): T[];
1122+
concat(...items: (T | T[] | ReadonlyArray<T>)[]): T[];
11221123
/**
11231124
* Adds all the elements of an array separated by the specified separator string.
11241125
* @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

Lines changed: 6 additions & 6 deletions
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: ReadonlyArray<string>[]): string[]; (...items: (string | ReadonlyArray<string>)[]): string[]; }
8+
>a.concat : { (...items: (string[] | ReadonlyArray<string>)[]): string[]; (...items: (string | string[] | ReadonlyArray<string>)[]): string[]; }
99
>a : string[]
10-
>concat : { (...items: ReadonlyArray<string>[]): string[]; (...items: (string | ReadonlyArray<string>)[]): string[]; }
10+
>concat : { (...items: (string[] | ReadonlyArray<string>)[]): string[]; (...items: (string | string[] | ReadonlyArray<string>)[]): string[]; }
1111
>"hello" : "hello"
1212
>'world' : "world"
1313

1414
a.concat('Hello');
1515
>a.concat('Hello') : string[]
16-
>a.concat : { (...items: ReadonlyArray<string>[]): string[]; (...items: (string | ReadonlyArray<string>)[]): string[]; }
16+
>a.concat : { (...items: (string[] | ReadonlyArray<string>)[]): string[]; (...items: (string | string[] | ReadonlyArray<string>)[]): string[]; }
1717
>a : string[]
18-
>concat : { (...items: ReadonlyArray<string>[]): string[]; (...items: (string | ReadonlyArray<string>)[]): string[]; }
18+
>concat : { (...items: (string[] | ReadonlyArray<string>)[]): string[]; (...items: (string | string[] | ReadonlyArray<string>)[]): string[]; }
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: ReadonlyArray<string>[]): string[]; (...items: (string | ReadonlyArray<string>)[]): string[]; }
28+
>b.concat : { (...items: (string[] | ReadonlyArray<string>)[]): string[]; (...items: (string | string[] | ReadonlyArray<string>)[]): string[]; }
2929
>b : string[]
30-
>concat : { (...items: ReadonlyArray<string>[]): string[]; (...items: (string | ReadonlyArray<string>)[]): string[]; }
30+
>concat : { (...items: (string[] | ReadonlyArray<string>)[]): string[]; (...items: (string | string[] | ReadonlyArray<string>)[]): string[]; }
3131
>'hello' : "hello"
3232

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [arrayConcat3.ts]
2+
// TODO: remove lib hack when https://github.com/Microsoft/TypeScript/issues/20454 is fixed
3+
type Fn<T extends object> = <U extends T>(subj: U) => U
4+
function doStuff<T extends object, T1 extends T>(a: Array<Fn<T>>, b: Array<Fn<T1>>) {
5+
b.concat(a);
6+
}
7+
8+
9+
//// [arrayConcat3.js]
10+
function doStuff(a, b) {
11+
b.concat(a);
12+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== tests/cases/compiler/arrayConcat3.ts ===
2+
// TODO: remove lib hack when https://github.com/Microsoft/TypeScript/issues/20454 is fixed
3+
type Fn<T extends object> = <U extends T>(subj: U) => U
4+
>Fn : Symbol(Fn, Decl(arrayConcat3.ts, 0, 0))
5+
>T : Symbol(T, Decl(arrayConcat3.ts, 1, 8))
6+
>U : Symbol(U, Decl(arrayConcat3.ts, 1, 29))
7+
>T : Symbol(T, Decl(arrayConcat3.ts, 1, 8))
8+
>subj : Symbol(subj, Decl(arrayConcat3.ts, 1, 42))
9+
>U : Symbol(U, Decl(arrayConcat3.ts, 1, 29))
10+
>U : Symbol(U, Decl(arrayConcat3.ts, 1, 29))
11+
12+
function doStuff<T extends object, T1 extends T>(a: Array<Fn<T>>, b: Array<Fn<T1>>) {
13+
>doStuff : Symbol(doStuff, Decl(arrayConcat3.ts, 1, 55))
14+
>T : Symbol(T, Decl(arrayConcat3.ts, 2, 17))
15+
>T1 : Symbol(T1, Decl(arrayConcat3.ts, 2, 34))
16+
>T : Symbol(T, Decl(arrayConcat3.ts, 2, 17))
17+
>a : Symbol(a, Decl(arrayConcat3.ts, 2, 49))
18+
>Array : Symbol(Array, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
19+
>Fn : Symbol(Fn, Decl(arrayConcat3.ts, 0, 0))
20+
>T : Symbol(T, Decl(arrayConcat3.ts, 2, 17))
21+
>b : Symbol(b, Decl(arrayConcat3.ts, 2, 65))
22+
>Array : Symbol(Array, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
23+
>Fn : Symbol(Fn, Decl(arrayConcat3.ts, 0, 0))
24+
>T1 : Symbol(T1, Decl(arrayConcat3.ts, 2, 34))
25+
26+
b.concat(a);
27+
>b.concat : Symbol(Array.concat, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
28+
>b : Symbol(b, Decl(arrayConcat3.ts, 2, 65))
29+
>concat : Symbol(Array.concat, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
30+
>a : Symbol(a, Decl(arrayConcat3.ts, 2, 49))
31+
}
32+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
=== tests/cases/compiler/arrayConcat3.ts ===
2+
// TODO: remove lib hack when https://github.com/Microsoft/TypeScript/issues/20454 is fixed
3+
type Fn<T extends object> = <U extends T>(subj: U) => U
4+
>Fn : Fn<T>
5+
>T : T
6+
>U : U
7+
>T : T
8+
>subj : U
9+
>U : U
10+
>U : U
11+
12+
function doStuff<T extends object, T1 extends T>(a: Array<Fn<T>>, b: Array<Fn<T1>>) {
13+
>doStuff : <T extends object, T1 extends T>(a: Fn<T>[], b: Fn<T1>[]) => void
14+
>T : T
15+
>T1 : T1
16+
>T : T
17+
>a : Fn<T>[]
18+
>Array : T[]
19+
>Fn : Fn<T>
20+
>T : T
21+
>b : Fn<T1>[]
22+
>Array : T[]
23+
>Fn : Fn<T>
24+
>T1 : T1
25+
26+
b.concat(a);
27+
>b.concat(a) : Fn<T1>[]
28+
>b.concat : { (...items: (Fn<T1>[] | ReadonlyArray<Fn<T1>>)[]): Fn<T1>[]; (...items: (Fn<T1> | Fn<T1>[] | ReadonlyArray<Fn<T1>>)[]): Fn<T1>[]; }
29+
>b : Fn<T1>[]
30+
>concat : { (...items: (Fn<T1>[] | ReadonlyArray<Fn<T1>>)[]): Fn<T1>[]; (...items: (Fn<T1> | Fn<T1>[] | ReadonlyArray<Fn<T1>>)[]): Fn<T1>[]; }
31+
>a : Fn<T>[]
32+
}
33+

tests/baselines/reference/arrayConcatMap.types

Lines changed: 2 additions & 2 deletions
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: ReadonlyArray<any>[]): any[]; (...items: any[]): any[]; }
7+
>[].concat : { (...items: (any[] | ReadonlyArray<any>)[]): any[]; (...items: any[]): any[]; }
88
>[] : undefined[]
9-
>concat : { (...items: ReadonlyArray<any>[]): any[]; (...items: any[]): any[]; }
9+
>concat : { (...items: (any[] | ReadonlyArray<any>)[]): any[]; (...items: any[]): any[]; }
1010
>[{ a: 1 }] : { a: number; }[]
1111
>{ a: 1 } : { a: number; }
1212
>a : number

tests/cases/compiler/arrayConcat3.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// TODO: remove lib hack when https://github.com/Microsoft/TypeScript/issues/20454 is fixed
2+
type Fn<T extends object> = <U extends T>(subj: U) => U
3+
function doStuff<T extends object, T1 extends T>(a: Array<Fn<T>>, b: Array<Fn<T1>>) {
4+
b.concat(a);
5+
}

0 commit comments

Comments
 (0)