Skip to content

Commit 3bcea0d

Browse files
jack-williamsDanielRosenwasser
authored andcommitted
Fix #35060 (#35065)
* Fix 35060 * Refactor and check ro-array to tuple case * Lint
1 parent 5321dcb commit 3bcea0d

8 files changed

+535
-26
lines changed

src/compiler/checker.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14528,32 +14528,33 @@ namespace ts {
1452814528
* if we have found an elaboration, or we should ignore
1452914529
* any other elaborations when relating the `source` and
1453014530
* `target` types.
14531-
*
14532-
* @param source
14533-
* @param target
14534-
* @param reportErrors
1453514531
*/
1453614532
function tryElaborateArrayLikeErrors(source: Type, target: Type, reportErrors: boolean): boolean {
14537-
if (isTupleLikeType(source)) {
14538-
const sourceTuple: TupleType | undefined = (source as TupleTypeReference).target;
14539-
if (sourceTuple && sourceTuple.readonly && isArrayOrTupleLikeType(target) &&
14540-
(!isReadonlyArrayType(target) || isTupleType(target) && !target.target.readonly)) {
14533+
/**
14534+
* The spec for elaboration is:
14535+
* - If the source is a readonly tuple and the target is a mutable array or tuple, elaborate on mutability and skip property elaborations.
14536+
* - If the source is a tuple then skip property elaborations if the target is an array or tuple.
14537+
* - If the source is a readonly array and the target is a mutable array or tuple, elaborate on mutability and skip property elaborations.
14538+
* - If the source an array then skip property elaborations if the target is a tuple.
14539+
*/
14540+
if (isTupleType(source)) {
14541+
if (source.target.readonly && isMutableArrayOrTuple(target)) {
1454114542
if (reportErrors) {
1454214543
reportError(Diagnostics.The_type_0_is_readonly_and_cannot_be_assigned_to_the_mutable_type_1, typeToString(source), typeToString(target));
1454314544
}
1454414545
return false;
1454514546
}
14546-
return isArrayLikeType(target);
14547+
return isTupleType(target) || isArrayType(target);
1454714548
}
14548-
if (isTupleLikeType(target)) {
14549-
return isArrayLikeType(source);
14550-
}
14551-
if (isReadonlyArrayType(source) && isArrayType(target) && !isReadonlyArrayType(target)) {
14549+
if (isReadonlyArrayType(source) && isMutableArrayOrTuple(target)) {
1455214550
if (reportErrors) {
1455314551
reportError(Diagnostics.The_type_0_is_readonly_and_cannot_be_assigned_to_the_mutable_type_1, typeToString(source), typeToString(target));
1455414552
}
1455514553
return false;
1455614554
}
14555+
if (isTupleType(target)) {
14556+
return isArrayType(source);
14557+
}
1455714558
return true;
1455814559
}
1455914560

@@ -16565,6 +16566,10 @@ namespace ts {
1656516566
return !!(getObjectFlags(type) & ObjectFlags.Reference) && (<TypeReference>type).target === globalReadonlyArrayType;
1656616567
}
1656716568

16569+
function isMutableArrayOrTuple(type: Type): boolean {
16570+
return isArrayType(type) && !isReadonlyArrayType(type) || isTupleType(type) && !type.target.readonly;
16571+
}
16572+
1656816573
function getElementTypeOfArrayType(type: Type): Type | undefined {
1656916574
return isArrayType(type) ? getTypeArguments(type as TypeReference)[0] : undefined;
1657016575
}

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/readonlyArraysAndTuples.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(12,12): error TS1
55
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(15,5): error TS4104: The type 'readonly string[]' is 'readonly' and cannot be assigned to the mutable type 'string[]'.
66
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(17,5): error TS4104: The type 'readonly [string, string]' is 'readonly' and cannot be assigned to the mutable type 'string[]'.
77
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(21,5): error TS2739: Type 'string[]' is missing the following properties from type '[string, string]': 0, 1
8-
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(22,5): error TS2740: Type 'readonly string[]' is missing the following properties from type '[string, string]': 0, 1, pop, push, and 5 more.
8+
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(22,5): error TS4104: The type 'readonly string[]' is 'readonly' and cannot be assigned to the mutable type '[string, string]'.
99
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(23,5): error TS4104: The type 'readonly [string, string]' is 'readonly' and cannot be assigned to the mutable type '[string, string]'.
1010
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(24,5): error TS2739: Type 'string[]' is missing the following properties from type 'readonly [string, string]': 0, 1
1111
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(25,5): error TS2739: Type 'readonly string[]' is missing the following properties from type 'readonly [string, string]': 0, 1
@@ -56,7 +56,7 @@ tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(36,8): error TS25
5656
!!! error TS2739: Type 'string[]' is missing the following properties from type '[string, string]': 0, 1
5757
mt = ra; // Error
5858
~~
59-
!!! error TS2740: Type 'readonly string[]' is missing the following properties from type '[string, string]': 0, 1, pop, push, and 5 more.
59+
!!! error TS4104: The type 'readonly string[]' is 'readonly' and cannot be assigned to the mutable type '[string, string]'.
6060
mt = rt; // Error
6161
~~
6262
!!! error TS4104: The type 'readonly [string, string]' is 'readonly' and cannot be assigned to the mutable type '[string, string]'.

0 commit comments

Comments
 (0)