Skip to content

Commit 393be5d

Browse files
committed
Fix 35060
1 parent 94f8590 commit 393be5d

7 files changed

+525
-22
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14531,25 +14531,24 @@ namespace ts {
1453114531
* if we have found an elaboration, or we should ignore
1453214532
* any other elaborations when relating the `source` and
1453314533
* `target` types.
14534-
*
14535-
* @param source
14536-
* @param target
14537-
* @param reportErrors
1453814534
*/
1453914535
function tryElaborateArrayLikeErrors(source: Type, target: Type, reportErrors: boolean): boolean {
14540-
if (isTupleLikeType(source)) {
14541-
const sourceTuple: TupleType | undefined = (source as TupleTypeReference).target;
14542-
if (sourceTuple && sourceTuple.readonly && isArrayOrTupleLikeType(target) &&
14543-
(!isReadonlyArrayType(target) || isTupleType(target) && !target.target.readonly)) {
14536+
if (isTupleType(source)) {
14537+
// If source is a readonly tuple
14538+
// and target is an array or tuple
14539+
// and target is not a readonly array
14540+
// and target is not a readonly tuple
14541+
if (source.target?.readonly && (isTupleType(target) || isArrayType(target)) &&
14542+
!isReadonlyArrayType(target) && !(isTupleType(target) && target.target.readonly)) {
1454414543
if (reportErrors) {
1454514544
reportError(Diagnostics.The_type_0_is_readonly_and_cannot_be_assigned_to_the_mutable_type_1, typeToString(source), typeToString(target));
1454614545
}
1454714546
return false;
1454814547
}
14549-
return isArrayLikeType(target);
14548+
return isTupleType(target) || isArrayType(target);
1455014549
}
14551-
if (isTupleLikeType(target)) {
14552-
return isArrayLikeType(source);
14550+
if (isTupleType(target)) {
14551+
return isArrayType(source);
1455314552
}
1455414553
if (isReadonlyArrayType(source) && isArrayType(target) && !isReadonlyArrayType(target)) {
1455514554
if (reportErrors) {

tests/baselines/reference/arityAndOrderCompatibility01.errors.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(15,12): erro
22
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(17,5): error TS2461: Type '{ 0: string; 1: number; length: 2; }' is not an array type.
33
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(18,5): error TS2741: Property '2' is missing in type '[string, number]' but required in type '[number, number, number]'.
44
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(19,5): error TS2741: Property '2' is missing in type 'StrNum' but required in type '[number, number, number]'.
5-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(20,5): error TS2740: Type '{ 0: string; 1: number; length: 2; }' is missing the following properties from type '[number, number, number]': 2, pop, push, concat, and 16 more.
5+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(20,5): error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[number, number, number]'.
66
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(21,5): error TS2741: Property '2' is missing in type '[string, number]' but required in type '[string, number, number]'.
77
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(22,5): error TS2741: Property '2' is missing in type 'StrNum' but required in type '[string, number, number]'.
8-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(23,5): error TS2740: Type '{ 0: string; 1: number; length: 2; }' is missing the following properties from type '[string, number, number]': 2, pop, push, concat, and 16 more.
8+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(23,5): error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[string, number, number]'.
99
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(24,5): error TS2322: Type '[string, number]' is not assignable to type '[number]'.
1010
Types of property '0' are incompatible.
1111
Type 'string' is not assignable to type 'number'.
1212
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(25,5): error TS2322: Type 'StrNum' is not assignable to type '[number]'.
1313
Types of property '0' are incompatible.
1414
Type 'string' is not assignable to type 'number'.
15-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(26,5): error TS2740: Type '{ 0: string; 1: number; length: 2; }' is missing the following properties from type '[number]': pop, push, concat, join, and 15 more.
15+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(26,5): error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[number]'.
1616
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(27,5): error TS2322: Type '[string, number]' is not assignable to type '[string]'.
1717
Types of property 'length' are incompatible.
1818
Type '2' is not assignable to type '1'.
1919
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(28,5): error TS2322: Type 'StrNum' is not assignable to type '[string]'.
2020
Types of property 'length' are incompatible.
2121
Type '2' is not assignable to type '1'.
22-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(29,5): error TS2740: Type '{ 0: string; 1: number; length: 2; }' is missing the following properties from type '[string]': pop, push, concat, join, and 15 more.
22+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(29,5): error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[string]'.
2323
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(30,5): error TS2322: Type '[string, number]' is not assignable to type '[number, string]'.
2424
Type 'string' is not assignable to type 'number'.
2525
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(31,5): error TS2322: Type 'StrNum' is not assignable to type '[number, string]'.
2626
Types of property '0' are incompatible.
2727
Type 'string' is not assignable to type 'number'.
28-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(32,5): error TS2740: Type '{ 0: string; 1: number; length: 2; }' is missing the following properties from type '[number, string]': pop, push, concat, join, and 15 more.
28+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(32,5): error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[number, string]'.
2929

3030

3131
==== tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts (17 errors) ====
@@ -58,7 +58,7 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(32,5): error
5858
!!! error TS2741: Property '2' is missing in type 'StrNum' but required in type '[number, number, number]'.
5959
var j3: [number, number, number] = z;
6060
~~
61-
!!! error TS2740: Type '{ 0: string; 1: number; length: 2; }' is missing the following properties from type '[number, number, number]': 2, pop, push, concat, and 16 more.
61+
!!! error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[number, number, number]'.
6262
var k1: [string, number, number] = x;
6363
~~
6464
!!! error TS2741: Property '2' is missing in type '[string, number]' but required in type '[string, number, number]'.
@@ -67,7 +67,7 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(32,5): error
6767
!!! error TS2741: Property '2' is missing in type 'StrNum' but required in type '[string, number, number]'.
6868
var k3: [string, number, number] = z;
6969
~~
70-
!!! error TS2740: Type '{ 0: string; 1: number; length: 2; }' is missing the following properties from type '[string, number, number]': 2, pop, push, concat, and 16 more.
70+
!!! error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[string, number, number]'.
7171
var l1: [number] = x;
7272
~~
7373
!!! error TS2322: Type '[string, number]' is not assignable to type '[number]'.
@@ -80,7 +80,7 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(32,5): error
8080
!!! error TS2322: Type 'string' is not assignable to type 'number'.
8181
var l3: [number] = z;
8282
~~
83-
!!! error TS2740: Type '{ 0: string; 1: number; length: 2; }' is missing the following properties from type '[number]': pop, push, concat, join, and 15 more.
83+
!!! error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[number]'.
8484
var m1: [string] = x;
8585
~~
8686
!!! error TS2322: Type '[string, number]' is not assignable to type '[string]'.
@@ -93,7 +93,7 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(32,5): error
9393
!!! error TS2322: Type '2' is not assignable to type '1'.
9494
var m3: [string] = z;
9595
~~
96-
!!! error TS2740: Type '{ 0: string; 1: number; length: 2; }' is missing the following properties from type '[string]': pop, push, concat, join, and 15 more.
96+
!!! error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[string]'.
9797
var n1: [number, string] = x;
9898
~~
9999
!!! error TS2322: Type '[string, number]' is not assignable to type '[number, string]'.
@@ -105,7 +105,7 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(32,5): error
105105
!!! error TS2322: Type 'string' is not assignable to type 'number'.
106106
var n3: [number, string] = z;
107107
~~
108-
!!! error TS2740: Type '{ 0: string; 1: number; length: 2; }' is missing the following properties from type '[number, string]': pop, push, concat, join, and 15 more.
108+
!!! error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[number, string]'.
109109
var o1: [string, number] = x;
110110
var o2: [string, number] = y;
111111
var o3: [string, number] = y;

tests/baselines/reference/readonlyTupleAndArrayElaboration.errors.txt

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,37 @@ tests/cases/compiler/readonlyTupleAndArrayElaboration.ts(23,9): error TS2345: Ar
1010
The type 'readonly number[]' is 'readonly' and cannot be assigned to the mutable type 'number[]'.
1111
tests/cases/compiler/readonlyTupleAndArrayElaboration.ts(24,9): error TS2345: Argument of type 'readonly number[]' is not assignable to parameter of type 'number[]'.
1212
The type 'readonly number[]' is 'readonly' and cannot be assigned to the mutable type 'number[]'.
13+
tests/cases/compiler/readonlyTupleAndArrayElaboration.ts(27,7): error TS2322: Type 'readonly [1]' is not assignable to type 'readonly []'.
14+
Types of property 'length' are incompatible.
15+
Type '1' is not assignable to type '0'.
16+
tests/cases/compiler/readonlyTupleAndArrayElaboration.ts(30,7): error TS4104: The type 'readonly [1]' is 'readonly' and cannot be assigned to the mutable type '[]'.
17+
tests/cases/compiler/readonlyTupleAndArrayElaboration.ts(33,7): error TS2322: Type '[1]' is not assignable to type 'readonly []'.
18+
Types of property 'length' are incompatible.
19+
Type '1' is not assignable to type '0'.
20+
tests/cases/compiler/readonlyTupleAndArrayElaboration.ts(36,7): error TS2322: Type '[1]' is not assignable to type '[]'.
21+
Types of property 'length' are incompatible.
22+
Type '1' is not assignable to type '0'.
23+
tests/cases/compiler/readonlyTupleAndArrayElaboration.ts(39,7): error TS2322: Type 'readonly number[]' is not assignable to type 'readonly boolean[]'.
24+
Type 'number' is not assignable to type 'boolean'.
25+
tests/cases/compiler/readonlyTupleAndArrayElaboration.ts(42,7): error TS4104: The type 'readonly number[]' is 'readonly' and cannot be assigned to the mutable type 'boolean[]'.
26+
tests/cases/compiler/readonlyTupleAndArrayElaboration.ts(45,7): error TS2322: Type 'number[]' is not assignable to type 'readonly boolean[]'.
27+
Type 'number' is not assignable to type 'boolean'.
28+
tests/cases/compiler/readonlyTupleAndArrayElaboration.ts(48,7): error TS2322: Type 'number[]' is not assignable to type 'boolean[]'.
29+
Type 'number' is not assignable to type 'boolean'.
30+
tests/cases/compiler/readonlyTupleAndArrayElaboration.ts(51,7): error TS2322: Type 'readonly [1]' is not assignable to type 'readonly boolean[]'.
31+
Type '1' is not assignable to type 'boolean'.
32+
tests/cases/compiler/readonlyTupleAndArrayElaboration.ts(54,7): error TS4104: The type 'readonly [1]' is 'readonly' and cannot be assigned to the mutable type 'number[]'.
33+
tests/cases/compiler/readonlyTupleAndArrayElaboration.ts(57,7): error TS2322: Type '[1]' is not assignable to type 'readonly boolean[]'.
34+
Type '1' is not assignable to type 'boolean'.
35+
tests/cases/compiler/readonlyTupleAndArrayElaboration.ts(60,7): error TS2322: Type '[1]' is not assignable to type 'boolean[]'.
36+
Type '1' is not assignable to type 'boolean'.
37+
tests/cases/compiler/readonlyTupleAndArrayElaboration.ts(63,7): error TS2741: Property '0' is missing in type 'readonly number[]' but required in type 'readonly [1]'.
38+
tests/cases/compiler/readonlyTupleAndArrayElaboration.ts(66,7): error TS2740: Type 'readonly number[]' is missing the following properties from type '[1]': 0, pop, push, reverse, and 4 more.
39+
tests/cases/compiler/readonlyTupleAndArrayElaboration.ts(69,7): error TS2741: Property '0' is missing in type 'number[]' but required in type 'readonly [1]'.
40+
tests/cases/compiler/readonlyTupleAndArrayElaboration.ts(72,7): error TS2741: Property '0' is missing in type 'number[]' but required in type '[1]'.
1341

1442

15-
==== tests/cases/compiler/readonlyTupleAndArrayElaboration.ts (6 errors) ====
43+
==== tests/cases/compiler/readonlyTupleAndArrayElaboration.ts (22 errors) ====
1644
// @strict
1745
// #Repro from #30839
1846

@@ -55,4 +83,96 @@ tests/cases/compiler/readonlyTupleAndArrayElaboration.ts(24,9): error TS2345: Ar
5583
~
5684
!!! error TS2345: Argument of type 'readonly number[]' is not assignable to parameter of type 'number[]'.
5785
!!! error TS2345: The type 'readonly number[]' is 'readonly' and cannot be assigned to the mutable type 'number[]'.
86+
87+
const t1: readonly [1] = [1];
88+
const t2: readonly [] = t1;
89+
~~
90+
!!! error TS2322: Type 'readonly [1]' is not assignable to type 'readonly []'.
91+
!!! error TS2322: Types of property 'length' are incompatible.
92+
!!! error TS2322: Type '1' is not assignable to type '0'.
93+
94+
const t3: readonly [1] = [1];
95+
const t4: [] = t3;
96+
~~
97+
!!! error TS4104: The type 'readonly [1]' is 'readonly' and cannot be assigned to the mutable type '[]'.
98+
99+
const t5: [1] = [1];
100+
const t6: readonly [] = t5;
101+
~~
102+
!!! error TS2322: Type '[1]' is not assignable to type 'readonly []'.
103+
!!! error TS2322: Types of property 'length' are incompatible.
104+
!!! error TS2322: Type '1' is not assignable to type '0'.
105+
106+
const t7: [1] = [1];
107+
const t8: [] = t7;
108+
~~
109+
!!! error TS2322: Type '[1]' is not assignable to type '[]'.
110+
!!! error TS2322: Types of property 'length' are incompatible.
111+
!!! error TS2322: Type '1' is not assignable to type '0'.
112+
113+
const a1: readonly number[] = [1];
114+
const a2: readonly boolean[] = a1;
115+
~~
116+
!!! error TS2322: Type 'readonly number[]' is not assignable to type 'readonly boolean[]'.
117+
!!! error TS2322: Type 'number' is not assignable to type 'boolean'.
118+
119+
const a3: readonly number[] = [1];
120+
const a4: boolean[] = a3;
121+
~~
122+
!!! error TS4104: The type 'readonly number[]' is 'readonly' and cannot be assigned to the mutable type 'boolean[]'.
123+
124+
const a5: number[] = [1];
125+
const a6: readonly boolean [] = a5;
126+
~~
127+
!!! error TS2322: Type 'number[]' is not assignable to type 'readonly boolean[]'.
128+
!!! error TS2322: Type 'number' is not assignable to type 'boolean'.
129+
130+
const a7: number[] = [1];
131+
const a8: boolean[] = a7;
132+
~~
133+
!!! error TS2322: Type 'number[]' is not assignable to type 'boolean[]'.
134+
!!! error TS2322: Type 'number' is not assignable to type 'boolean'.
135+
136+
const ta1: readonly [1] = [1];
137+
const ta2: readonly boolean[] = ta1;
138+
~~~
139+
!!! error TS2322: Type 'readonly [1]' is not assignable to type 'readonly boolean[]'.
140+
!!! error TS2322: Type '1' is not assignable to type 'boolean'.
141+
142+
const ta3: readonly [1] = [1];
143+
const ta4: number[] = ta3;
144+
~~~
145+
!!! error TS4104: The type 'readonly [1]' is 'readonly' and cannot be assigned to the mutable type 'number[]'.
146+
147+
const ta5: [1] = [1];
148+
const ta6: readonly boolean[] = ta5;
149+
~~~
150+
!!! error TS2322: Type '[1]' is not assignable to type 'readonly boolean[]'.
151+
!!! error TS2322: Type '1' is not assignable to type 'boolean'.
152+
153+
const ta7: [1] = [1];
154+
const ta8: boolean[] = ta7;
155+
~~~
156+
!!! error TS2322: Type '[1]' is not assignable to type 'boolean[]'.
157+
!!! error TS2322: Type '1' is not assignable to type 'boolean'.
158+
159+
const at1: readonly number[] = [1];
160+
const at2: readonly [1] = at1;
161+
~~~
162+
!!! error TS2741: Property '0' is missing in type 'readonly number[]' but required in type 'readonly [1]'.
163+
164+
const at3: readonly number[] = [1];
165+
const at4: [1] = at3;
166+
~~~
167+
!!! error TS2740: Type 'readonly number[]' is missing the following properties from type '[1]': 0, pop, push, reverse, and 4 more.
168+
169+
const at5: number[] = [1];
170+
const at6: readonly [1] = at5;
171+
~~~
172+
!!! error TS2741: Property '0' is missing in type 'number[]' but required in type 'readonly [1]'.
173+
174+
const at7: number[] = [1];
175+
const at8: [1] = at7;
176+
~~~
177+
!!! error TS2741: Property '0' is missing in type 'number[]' but required in type '[1]'.
58178

tests/baselines/reference/readonlyTupleAndArrayElaboration.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,54 @@ declare const c: ReadonlyArray<number>;
2323
arryFn2(a);
2424
arryFn2(b);
2525
arryFn2(c);
26+
27+
const t1: readonly [1] = [1];
28+
const t2: readonly [] = t1;
29+
30+
const t3: readonly [1] = [1];
31+
const t4: [] = t3;
32+
33+
const t5: [1] = [1];
34+
const t6: readonly [] = t5;
35+
36+
const t7: [1] = [1];
37+
const t8: [] = t7;
38+
39+
const a1: readonly number[] = [1];
40+
const a2: readonly boolean[] = a1;
41+
42+
const a3: readonly number[] = [1];
43+
const a4: boolean[] = a3;
44+
45+
const a5: number[] = [1];
46+
const a6: readonly boolean [] = a5;
47+
48+
const a7: number[] = [1];
49+
const a8: boolean[] = a7;
50+
51+
const ta1: readonly [1] = [1];
52+
const ta2: readonly boolean[] = ta1;
53+
54+
const ta3: readonly [1] = [1];
55+
const ta4: number[] = ta3;
56+
57+
const ta5: [1] = [1];
58+
const ta6: readonly boolean[] = ta5;
59+
60+
const ta7: [1] = [1];
61+
const ta8: boolean[] = ta7;
62+
63+
const at1: readonly number[] = [1];
64+
const at2: readonly [1] = at1;
65+
66+
const at3: readonly number[] = [1];
67+
const at4: [1] = at3;
68+
69+
const at5: number[] = [1];
70+
const at6: readonly [1] = at5;
71+
72+
const at7: number[] = [1];
73+
const at8: [1] = at7;
2674

2775

2876
//// [readonlyTupleAndArrayElaboration.js]
@@ -39,3 +87,35 @@ arryFn2(point);
3987
arryFn2(a);
4088
arryFn2(b);
4189
arryFn2(c);
90+
var t1 = [1];
91+
var t2 = t1;
92+
var t3 = [1];
93+
var t4 = t3;
94+
var t5 = [1];
95+
var t6 = t5;
96+
var t7 = [1];
97+
var t8 = t7;
98+
var a1 = [1];
99+
var a2 = a1;
100+
var a3 = [1];
101+
var a4 = a3;
102+
var a5 = [1];
103+
var a6 = a5;
104+
var a7 = [1];
105+
var a8 = a7;
106+
var ta1 = [1];
107+
var ta2 = ta1;
108+
var ta3 = [1];
109+
var ta4 = ta3;
110+
var ta5 = [1];
111+
var ta6 = ta5;
112+
var ta7 = [1];
113+
var ta8 = ta7;
114+
var at1 = [1];
115+
var at2 = at1;
116+
var at3 = [1];
117+
var at4 = at3;
118+
var at5 = [1];
119+
var at6 = at5;
120+
var at7 = [1];
121+
var at8 = at7;

0 commit comments

Comments
 (0)