Skip to content

Commit 0006371

Browse files
authored
Merge pull request #10374 from Microsoft/readonly-array-type-argument-assignability
Improve ReadonlyArray<T>.concat to match Array<T>
2 parents c9d1f0d + ce5e207 commit 0006371

File tree

4 files changed

+124
-1
lines changed

4 files changed

+124
-1
lines changed

src/lib/es5.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,12 @@ interface ReadonlyArray<T> {
10061006
* Combines two or more arrays.
10071007
* @param items Additional items to add to the end of array1.
10081008
*/
1009-
concat(...items: T[]): T[];
1009+
concat(...items: T[][]): T[];
1010+
/**
1011+
* Combines two or more arrays.
1012+
* @param items Additional items to add to the end of array1.
1013+
*/
1014+
concat(...items: (T | T[])[]): T[];
10101015
/**
10111016
* Adds all the elements of an array separated by the specified separator string.
10121017
* @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.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts(13,1): error TS2322: Type 'A[]' is not assignable to type 'ReadonlyArray<B>'.
2+
Types of property 'concat' are incompatible.
3+
Type '{ (...items: A[][]): A[]; (...items: (A | A[])[]): A[]; }' is not assignable to type '{ <U extends ReadonlyArray<B>>(...items: U[]): B[]; (...items: B[][]): B[]; (...items: (B | B[])[]): B[]; }'.
4+
Type 'A[]' is not assignable to type 'B[]'.
5+
Type 'A' is not assignable to type 'B'.
6+
Property 'b' is missing in type 'A'.
7+
tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts(18,1): error TS2322: Type 'C<A>' is not assignable to type 'ReadonlyArray<B>'.
8+
Types of property 'concat' are incompatible.
9+
Type '{ (...items: A[][]): A[]; (...items: (A | A[])[]): A[]; }' is not assignable to type '{ <U extends ReadonlyArray<B>>(...items: U[]): B[]; (...items: B[][]): B[]; (...items: (B | B[])[]): B[]; }'.
10+
Type 'A[]' is not assignable to type 'B[]'.
11+
Type 'A' is not assignable to type 'B'.
12+
13+
14+
==== tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts (2 errors) ====
15+
class A { a }
16+
class B extends A { b }
17+
class C<T> extends Array<T> { c }
18+
declare var ara: A[];
19+
declare var arb: B[];
20+
declare var cra: C<A>;
21+
declare var crb: C<B>;
22+
declare var rra: ReadonlyArray<A>;
23+
declare var rrb: ReadonlyArray<B>;
24+
rra = ara;
25+
rrb = arb; // OK, Array<B> is assignable to ReadonlyArray<A>
26+
rra = arb;
27+
rrb = ara; // error: 'A' is not assignable to 'B'
28+
~~~
29+
!!! error TS2322: Type 'A[]' is not assignable to type 'ReadonlyArray<B>'.
30+
!!! error TS2322: Types of property 'concat' are incompatible.
31+
!!! error TS2322: Type '{ (...items: A[][]): A[]; (...items: (A | A[])[]): A[]; }' is not assignable to type '{ <U extends ReadonlyArray<B>>(...items: U[]): B[]; (...items: B[][]): B[]; (...items: (B | B[])[]): B[]; }'.
32+
!!! error TS2322: Type 'A[]' is not assignable to type 'B[]'.
33+
!!! error TS2322: Type 'A' is not assignable to type 'B'.
34+
!!! error TS2322: Property 'b' is missing in type 'A'.
35+
36+
rra = cra;
37+
rra = crb; // OK, C<B> is assignable to ReadonlyArray<A>
38+
rrb = crb;
39+
rrb = cra; // error: 'A' is not assignable to 'B'
40+
~~~
41+
!!! error TS2322: Type 'C<A>' is not assignable to type 'ReadonlyArray<B>'.
42+
!!! error TS2322: Types of property 'concat' are incompatible.
43+
!!! error TS2322: Type '{ (...items: A[][]): A[]; (...items: (A | A[])[]): A[]; }' is not assignable to type '{ <U extends ReadonlyArray<B>>(...items: U[]): B[]; (...items: B[][]): B[]; (...items: (B | B[])[]): B[]; }'.
44+
!!! error TS2322: Type 'A[]' is not assignable to type 'B[]'.
45+
!!! error TS2322: Type 'A' is not assignable to type 'B'.
46+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//// [arrayOfSubtypeIsAssignableToReadonlyArray.ts]
2+
class A { a }
3+
class B extends A { b }
4+
class C<T> extends Array<T> { c }
5+
declare var ara: A[];
6+
declare var arb: B[];
7+
declare var cra: C<A>;
8+
declare var crb: C<B>;
9+
declare var rra: ReadonlyArray<A>;
10+
declare var rrb: ReadonlyArray<B>;
11+
rra = ara;
12+
rrb = arb; // OK, Array<B> is assignable to ReadonlyArray<A>
13+
rra = arb;
14+
rrb = ara; // error: 'A' is not assignable to 'B'
15+
16+
rra = cra;
17+
rra = crb; // OK, C<B> is assignable to ReadonlyArray<A>
18+
rrb = crb;
19+
rrb = cra; // error: 'A' is not assignable to 'B'
20+
21+
22+
//// [arrayOfSubtypeIsAssignableToReadonlyArray.js]
23+
var __extends = (this && this.__extends) || function (d, b) {
24+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
25+
function __() { this.constructor = d; }
26+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
27+
};
28+
var A = (function () {
29+
function A() {
30+
}
31+
return A;
32+
}());
33+
var B = (function (_super) {
34+
__extends(B, _super);
35+
function B() {
36+
_super.apply(this, arguments);
37+
}
38+
return B;
39+
}(A));
40+
var C = (function (_super) {
41+
__extends(C, _super);
42+
function C() {
43+
_super.apply(this, arguments);
44+
}
45+
return C;
46+
}(Array));
47+
rra = ara;
48+
rrb = arb; // OK, Array<B> is assignable to ReadonlyArray<A>
49+
rra = arb;
50+
rrb = ara; // error: 'A' is not assignable to 'B'
51+
rra = cra;
52+
rra = crb; // OK, C<B> is assignable to ReadonlyArray<A>
53+
rrb = crb;
54+
rrb = cra; // error: 'A' is not assignable to 'B'
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class A { a }
2+
class B extends A { b }
3+
class C<T> extends Array<T> { c }
4+
declare var ara: A[];
5+
declare var arb: B[];
6+
declare var cra: C<A>;
7+
declare var crb: C<B>;
8+
declare var rra: ReadonlyArray<A>;
9+
declare var rrb: ReadonlyArray<B>;
10+
rra = ara;
11+
rrb = arb; // OK, Array<B> is assignable to ReadonlyArray<A>
12+
rra = arb;
13+
rrb = ara; // error: 'A' is not assignable to 'B'
14+
15+
rra = cra;
16+
rra = crb; // OK, C<B> is assignable to ReadonlyArray<A>
17+
rrb = crb;
18+
rrb = cra; // error: 'A' is not assignable to 'B'

0 commit comments

Comments
 (0)