Skip to content

Commit 12e371b

Browse files
Merge pull request #28313 from a-tarasyuk/feature/28297-make-error-message-at--a-constant-or-a-read-only-property--more-specific
Feature/28297 make error message at a constant or a read only property more specific
2 parents bc5668e + d3d2114 commit 12e371b

39 files changed

+329
-320
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9455,7 +9455,7 @@ namespace ts {
94559455
if (accessExpression) {
94569456
markPropertyAsReferenced(prop, accessExpression, /*isThisAccess*/ accessExpression.expression.kind === SyntaxKind.ThisKeyword);
94579457
if (isAssignmentTarget(accessExpression) && (isReferenceToReadonlyEntity(accessExpression, prop) || isReferenceThroughNamespaceImport(accessExpression))) {
9458-
error(accessExpression.argumentExpression, Diagnostics.Cannot_assign_to_0_because_it_is_a_constant_or_a_read_only_property, symbolToString(prop));
9458+
error(accessExpression.argumentExpression, Diagnostics.Cannot_assign_to_0_because_it_is_a_read_only_property, symbolToString(prop));
94599459
return missingType;
94609460
}
94619461
if (cacheSymbol) {
@@ -16115,7 +16115,12 @@ namespace ts {
1611516115
return errorType;
1611616116
}
1611716117
if (isReadonlySymbol(localOrExportSymbol)) {
16118-
error(node, Diagnostics.Cannot_assign_to_0_because_it_is_a_constant_or_a_read_only_property, symbolToString(symbol));
16118+
if (localOrExportSymbol.flags & SymbolFlags.Variable) {
16119+
error(node, Diagnostics.Cannot_assign_to_0_because_it_is_a_constant, symbolToString(symbol));
16120+
}
16121+
else {
16122+
error(node, Diagnostics.Cannot_assign_to_0_because_it_is_a_read_only_property, symbolToString(symbol));
16123+
}
1611916124
return errorType;
1612016125
}
1612116126
}
@@ -18700,7 +18705,7 @@ namespace ts {
1870018705
checkPropertyAccessibility(node, left.kind === SyntaxKind.SuperKeyword, apparentType, prop);
1870118706
if (assignmentKind) {
1870218707
if (isReferenceToReadonlyEntity(<Expression>node, prop) || isReferenceThroughNamespaceImport(<Expression>node)) {
18703-
error(right, Diagnostics.Cannot_assign_to_0_because_it_is_a_constant_or_a_read_only_property, idText(right));
18708+
error(right, Diagnostics.Cannot_assign_to_0_because_it_is_a_read_only_property, idText(right));
1870418709
return errorType;
1870518710
}
1870618711
}

src/compiler/diagnosticMessages.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1944,7 +1944,7 @@
19441944
"category": "Error",
19451945
"code": 2539
19461946
},
1947-
"Cannot assign to '{0}' because it is a constant or a read-only property.": {
1947+
"Cannot assign to '{0}' because it is a read-only property.": {
19481948
"category": "Error",
19491949
"code": 2540
19501950
},
@@ -2124,6 +2124,10 @@
21242124
"category": "Error",
21252125
"code": 2587
21262126
},
2127+
"Cannot assign to '{0}' because it is a constant.": {
2128+
"category": "Error",
2129+
"code": 2588
2130+
},
21272131
"JSX element attributes type '{0}' may not be a union type.": {
21282132
"category": "Error",
21292133
"code": 2600

tests/baselines/reference/abstractPropertyNegative.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ tests/cases/compiler/abstractPropertyNegative.ts(13,7): error TS2515: Non-abstra
66
tests/cases/compiler/abstractPropertyNegative.ts(13,7): error TS2515: Non-abstract class 'C' does not implement inherited abstract member 'readonlyProp' from class 'B'.
77
tests/cases/compiler/abstractPropertyNegative.ts(15,5): error TS1244: Abstract methods can only appear within an abstract class.
88
tests/cases/compiler/abstractPropertyNegative.ts(16,37): error TS1005: '{' expected.
9-
tests/cases/compiler/abstractPropertyNegative.ts(19,3): error TS2540: Cannot assign to 'ro' because it is a constant or a read-only property.
9+
tests/cases/compiler/abstractPropertyNegative.ts(19,3): error TS2540: Cannot assign to 'ro' because it is a read-only property.
1010
tests/cases/compiler/abstractPropertyNegative.ts(25,5): error TS2416: Property 'num' in type 'WrongTypePropertyImpl' is not assignable to the same property in base type 'WrongTypeProperty'.
1111
Type 'string' is not assignable to type 'number'.
1212
tests/cases/compiler/abstractPropertyNegative.ts(31,9): error TS2416: Property 'num' in type 'WrongTypeAccessorImpl' is not assignable to the same property in base type 'WrongTypeAccessor'.
@@ -56,7 +56,7 @@ tests/cases/compiler/abstractPropertyNegative.ts(41,18): error TS2676: Accessors
5656
let c = new C();
5757
c.ro = "error: lhs of assignment can't be readonly";
5858
~~
59-
!!! error TS2540: Cannot assign to 'ro' because it is a constant or a read-only property.
59+
!!! error TS2540: Cannot assign to 'ro' because it is a read-only property.
6060

6161
abstract class WrongTypeProperty {
6262
abstract num: number;

tests/baselines/reference/assignToEnum.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
tests/cases/compiler/assignToEnum.ts(2,1): error TS2539: Cannot assign to 'A' because it is not a variable.
22
tests/cases/compiler/assignToEnum.ts(3,1): error TS2539: Cannot assign to 'A' because it is not a variable.
3-
tests/cases/compiler/assignToEnum.ts(4,3): error TS2540: Cannot assign to 'foo' because it is a constant or a read-only property.
4-
tests/cases/compiler/assignToEnum.ts(5,3): error TS2540: Cannot assign to 'foo' because it is a constant or a read-only property.
3+
tests/cases/compiler/assignToEnum.ts(4,3): error TS2540: Cannot assign to 'foo' because it is a read-only property.
4+
tests/cases/compiler/assignToEnum.ts(5,3): error TS2540: Cannot assign to 'foo' because it is a read-only property.
55

66

77
==== tests/cases/compiler/assignToEnum.ts (4 errors) ====
@@ -14,9 +14,9 @@ tests/cases/compiler/assignToEnum.ts(5,3): error TS2540: Cannot assign to 'foo'
1414
!!! error TS2539: Cannot assign to 'A' because it is not a variable.
1515
A.foo = 1; // invalid LHS
1616
~~~
17-
!!! error TS2540: Cannot assign to 'foo' because it is a constant or a read-only property.
17+
!!! error TS2540: Cannot assign to 'foo' because it is a read-only property.
1818
A.foo = A.bar; // invalid LHS
1919
~~~
20-
!!! error TS2540: Cannot assign to 'foo' because it is a constant or a read-only property.
20+
!!! error TS2540: Cannot assign to 'foo' because it is a read-only property.
2121

2222

tests/baselines/reference/assignments.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(11,1): error TS2708: Cannot use namespace 'M' as a value.
22
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(14,1): error TS2539: Cannot assign to 'C' because it is not a variable.
33
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(17,1): error TS2539: Cannot assign to 'E' because it is not a variable.
4-
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(18,3): error TS2540: Cannot assign to 'A' because it is a constant or a read-only property.
4+
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(18,3): error TS2540: Cannot assign to 'A' because it is a read-only property.
55
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(21,1): error TS2539: Cannot assign to 'fn' because it is not a variable.
66
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(31,1): error TS2693: 'I' only refers to a type, but is being used as a value here.
77

@@ -32,7 +32,7 @@ tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(31,1): er
3232
!!! error TS2539: Cannot assign to 'E' because it is not a variable.
3333
E.A = null; // OK per spec, Error per implementation (509581)
3434
~
35-
!!! error TS2540: Cannot assign to 'A' because it is a constant or a read-only property.
35+
!!! error TS2540: Cannot assign to 'A' because it is a read-only property.
3636

3737
function fn() { }
3838
fn = null; // Should be error

tests/baselines/reference/bigintWithLib.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
tests/cases/compiler/bigintWithLib.ts(4,1): error TS2350: Only a void function can be called with the 'new' keyword.
22
tests/cases/compiler/bigintWithLib.ts(16,33): error TS2345: Argument of type 'number[]' is not assignable to parameter of type 'ArrayBuffer | SharedArrayBuffer'.
33
Type 'number[]' is missing the following properties from type 'SharedArrayBuffer': byteLength, [Symbol.species], [Symbol.toStringTag]
4-
tests/cases/compiler/bigintWithLib.ts(21,13): error TS2540: Cannot assign to 'length' because it is a constant or a read-only property.
4+
tests/cases/compiler/bigintWithLib.ts(21,13): error TS2540: Cannot assign to 'length' because it is a read-only property.
55
tests/cases/compiler/bigintWithLib.ts(28,35): error TS2345: Argument of type 'number[]' is not assignable to parameter of type 'ArrayBuffer | SharedArrayBuffer'.
66
Type 'number[]' is not assignable to type 'SharedArrayBuffer'.
7-
tests/cases/compiler/bigintWithLib.ts(33,13): error TS2540: Cannot assign to 'length' because it is a constant or a read-only property.
7+
tests/cases/compiler/bigintWithLib.ts(33,13): error TS2540: Cannot assign to 'length' because it is a read-only property.
88
tests/cases/compiler/bigintWithLib.ts(40,25): error TS2345: Argument of type '-1' is not assignable to parameter of type 'bigint'.
99
tests/cases/compiler/bigintWithLib.ts(43,26): error TS2345: Argument of type '123' is not assignable to parameter of type 'bigint'.
1010

@@ -37,7 +37,7 @@ tests/cases/compiler/bigintWithLib.ts(43,26): error TS2345: Argument of type '12
3737
let len: number = bigIntArray.length;
3838
bigIntArray.length = 10; // should error
3939
~~~~~~
40-
!!! error TS2540: Cannot assign to 'length' because it is a constant or a read-only property.
40+
!!! error TS2540: Cannot assign to 'length' because it is a read-only property.
4141
let arrayBufferLike: ArrayBufferView = bigIntArray;
4242

4343
// Test BigUint64Array
@@ -54,7 +54,7 @@ tests/cases/compiler/bigintWithLib.ts(43,26): error TS2345: Argument of type '12
5454
len = bigIntArray.length;
5555
bigIntArray.length = 10; // should error
5656
~~~~~~
57-
!!! error TS2540: Cannot assign to 'length' because it is a constant or a read-only property.
57+
!!! error TS2540: Cannot assign to 'length' because it is a read-only property.
5858
arrayBufferLike = bigIntArray;
5959

6060
// Test added DataView methods

tests/baselines/reference/checkExportsObjectAssignProperty.errors.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
tests/cases/conformance/jsdoc/validator.ts(17,4): error TS2540: Cannot assign to 'readonlyProp' because it is a constant or a read-only property.
2-
tests/cases/conformance/jsdoc/validator.ts(18,4): error TS2540: Cannot assign to 'readonlyAccessor' because it is a constant or a read-only property.
1+
tests/cases/conformance/jsdoc/validator.ts(17,4): error TS2540: Cannot assign to 'readonlyProp' because it is a read-only property.
2+
tests/cases/conformance/jsdoc/validator.ts(18,4): error TS2540: Cannot assign to 'readonlyAccessor' because it is a read-only property.
33
tests/cases/conformance/jsdoc/validator.ts(19,1): error TS2322: Type '"no"' is not assignable to type 'number'.
44
tests/cases/conformance/jsdoc/validator.ts(20,1): error TS2322: Type '"no"' is not assignable to type 'number'.
55
tests/cases/conformance/jsdoc/validator.ts(21,1): error TS2322: Type '0' is not assignable to type 'string'.
6-
tests/cases/conformance/jsdoc/validator.ts(37,4): error TS2540: Cannot assign to 'readonlyProp' because it is a constant or a read-only property.
7-
tests/cases/conformance/jsdoc/validator.ts(38,4): error TS2540: Cannot assign to 'readonlyAccessor' because it is a constant or a read-only property.
6+
tests/cases/conformance/jsdoc/validator.ts(37,4): error TS2540: Cannot assign to 'readonlyProp' because it is a read-only property.
7+
tests/cases/conformance/jsdoc/validator.ts(38,4): error TS2540: Cannot assign to 'readonlyAccessor' because it is a read-only property.
88
tests/cases/conformance/jsdoc/validator.ts(39,1): error TS2322: Type '0' is not assignable to type 'string'.
99
tests/cases/conformance/jsdoc/validator.ts(40,1): error TS2322: Type '"no"' is not assignable to type 'number'.
1010
tests/cases/conformance/jsdoc/validator.ts(41,1): error TS2322: Type '0' is not assignable to type 'string'.
@@ -29,10 +29,10 @@ tests/cases/conformance/jsdoc/validator.ts(41,1): error TS2322: Type '0' is not
2929
// disallowed assignments
3030
m1.readonlyProp = "name";
3131
~~~~~~~~~~~~
32-
!!! error TS2540: Cannot assign to 'readonlyProp' because it is a constant or a read-only property.
32+
!!! error TS2540: Cannot assign to 'readonlyProp' because it is a read-only property.
3333
m1.readonlyAccessor = 12;
3434
~~~~~~~~~~~~~~~~
35-
!!! error TS2540: Cannot assign to 'readonlyAccessor' because it is a constant or a read-only property.
35+
!!! error TS2540: Cannot assign to 'readonlyAccessor' because it is a read-only property.
3636
m1.thing = "no";
3737
~~~~~~~~
3838
!!! error TS2322: Type '"no"' is not assignable to type 'number'.
@@ -59,10 +59,10 @@ tests/cases/conformance/jsdoc/validator.ts(41,1): error TS2322: Type '0' is not
5959
// disallowed assignments
6060
m2.readonlyProp = "name";
6161
~~~~~~~~~~~~
62-
!!! error TS2540: Cannot assign to 'readonlyProp' because it is a constant or a read-only property.
62+
!!! error TS2540: Cannot assign to 'readonlyProp' because it is a read-only property.
6363
m2.readonlyAccessor = 12;
6464
~~~~~~~~~~~~~~~~
65-
!!! error TS2540: Cannot assign to 'readonlyAccessor' because it is a constant or a read-only property.
65+
!!! error TS2540: Cannot assign to 'readonlyAccessor' because it is a read-only property.
6666
m2.thing = 0;
6767
~~~~~~~~
6868
!!! error TS2322: Type '0' is not assignable to type 'string'.

tests/baselines/reference/checkExportsObjectAssignPrototypeProperty.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tests/cases/conformance/jsdoc/validator.ts(19,4): error TS2540: Cannot assign to 'readonlyProp' because it is a constant or a read-only property.
2-
tests/cases/conformance/jsdoc/validator.ts(20,4): error TS2540: Cannot assign to 'readonlyAccessor' because it is a constant or a read-only property.
1+
tests/cases/conformance/jsdoc/validator.ts(19,4): error TS2540: Cannot assign to 'readonlyProp' because it is a read-only property.
2+
tests/cases/conformance/jsdoc/validator.ts(20,4): error TS2540: Cannot assign to 'readonlyAccessor' because it is a read-only property.
33
tests/cases/conformance/jsdoc/validator.ts(21,1): error TS2322: Type '"no"' is not assignable to type 'number'.
44
tests/cases/conformance/jsdoc/validator.ts(22,1): error TS2322: Type '"no"' is not assignable to type 'number'.
55
tests/cases/conformance/jsdoc/validator.ts(23,1): error TS2322: Type '0' is not assignable to type 'string'.
@@ -26,10 +26,10 @@ tests/cases/conformance/jsdoc/validator.ts(23,1): error TS2322: Type '0' is not
2626
// disallowed assignments
2727
m1.readonlyProp = "name";
2828
~~~~~~~~~~~~
29-
!!! error TS2540: Cannot assign to 'readonlyProp' because it is a constant or a read-only property.
29+
!!! error TS2540: Cannot assign to 'readonlyProp' because it is a read-only property.
3030
m1.readonlyAccessor = 12;
3131
~~~~~~~~~~~~~~~~
32-
!!! error TS2540: Cannot assign to 'readonlyAccessor' because it is a constant or a read-only property.
32+
!!! error TS2540: Cannot assign to 'readonlyAccessor' because it is a read-only property.
3333
m1.thing = "no";
3434
~~~~~~~~
3535
!!! error TS2322: Type '"no"' is not assignable to type 'number'.

tests/baselines/reference/checkObjectDefineProperty.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
tests/cases/conformance/jsdoc/validate.ts(14,3): error TS2540: Cannot assign to 'lastName' because it is a constant or a read-only property.
2-
tests/cases/conformance/jsdoc/validate.ts(15,3): error TS2540: Cannot assign to 'houseNumber' because it is a constant or a read-only property.
1+
tests/cases/conformance/jsdoc/validate.ts(14,3): error TS2540: Cannot assign to 'lastName' because it is a read-only property.
2+
tests/cases/conformance/jsdoc/validate.ts(15,3): error TS2540: Cannot assign to 'houseNumber' because it is a read-only property.
33
tests/cases/conformance/jsdoc/validate.ts(16,1): error TS2322: Type '12' is not assignable to type 'string'.
4-
tests/cases/conformance/jsdoc/validate.ts(17,3): error TS2540: Cannot assign to 'middleInit' because it is a constant or a read-only property.
4+
tests/cases/conformance/jsdoc/validate.ts(17,3): error TS2540: Cannot assign to 'middleInit' because it is a read-only property.
55

66

77
==== tests/cases/conformance/jsdoc/validate.ts (4 errors) ====
@@ -20,16 +20,16 @@ tests/cases/conformance/jsdoc/validate.ts(17,3): error TS2540: Cannot assign to
2020

2121
x.lastName = "should fail";
2222
~~~~~~~~
23-
!!! error TS2540: Cannot assign to 'lastName' because it is a constant or a read-only property.
23+
!!! error TS2540: Cannot assign to 'lastName' because it is a read-only property.
2424
x.houseNumber = 12; // should also fail
2525
~~~~~~~~~~~
26-
!!! error TS2540: Cannot assign to 'houseNumber' because it is a constant or a read-only property.
26+
!!! error TS2540: Cannot assign to 'houseNumber' because it is a read-only property.
2727
x.zipStr = 12; // should fail
2828
~~~~~~~~
2929
!!! error TS2322: Type '12' is not assignable to type 'string'.
3030
x.middleInit = "R"; // should also fail
3131
~~~~~~~~~~
32-
!!! error TS2540: Cannot assign to 'middleInit' because it is a constant or a read-only property.
32+
!!! error TS2540: Cannot assign to 'middleInit' because it is a read-only property.
3333

3434
==== tests/cases/conformance/jsdoc/index.js (0 errors) ====
3535
const x = {};

tests/baselines/reference/checkOtherObjectAssignProperty.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ tests/cases/conformance/jsdoc/importer.js(3,5): error TS2339: Property 'other' d
22
tests/cases/conformance/jsdoc/importer.js(4,5): error TS2339: Property 'prop' does not exist on type 'typeof import("tests/cases/conformance/jsdoc/mod1")'.
33
tests/cases/conformance/jsdoc/importer.js(11,5): error TS2339: Property 'other' does not exist on type 'typeof import("tests/cases/conformance/jsdoc/mod1")'.
44
tests/cases/conformance/jsdoc/importer.js(12,5): error TS2339: Property 'prop' does not exist on type 'typeof import("tests/cases/conformance/jsdoc/mod1")'.
5-
tests/cases/conformance/jsdoc/importer.js(13,5): error TS2540: Cannot assign to 'bad1' because it is a constant or a read-only property.
6-
tests/cases/conformance/jsdoc/importer.js(14,5): error TS2540: Cannot assign to 'bad2' because it is a constant or a read-only property.
7-
tests/cases/conformance/jsdoc/importer.js(15,5): error TS2540: Cannot assign to 'bad3' because it is a constant or a read-only property.
5+
tests/cases/conformance/jsdoc/importer.js(13,5): error TS2540: Cannot assign to 'bad1' because it is a read-only property.
6+
tests/cases/conformance/jsdoc/importer.js(14,5): error TS2540: Cannot assign to 'bad2' because it is a read-only property.
7+
tests/cases/conformance/jsdoc/importer.js(15,5): error TS2540: Cannot assign to 'bad3' because it is a read-only property.
88

99

1010
==== tests/cases/conformance/jsdoc/importer.js (7 errors) ====
@@ -30,13 +30,13 @@ tests/cases/conformance/jsdoc/importer.js(15,5): error TS2540: Cannot assign to
3030
!!! error TS2339: Property 'prop' does not exist on type 'typeof import("tests/cases/conformance/jsdoc/mod1")'.
3131
mod.bad1 = 0;
3232
~~~~
33-
!!! error TS2540: Cannot assign to 'bad1' because it is a constant or a read-only property.
33+
!!! error TS2540: Cannot assign to 'bad1' because it is a read-only property.
3434
mod.bad2 = 0;
3535
~~~~
36-
!!! error TS2540: Cannot assign to 'bad2' because it is a constant or a read-only property.
36+
!!! error TS2540: Cannot assign to 'bad2' because it is a read-only property.
3737
mod.bad3 = 0;
3838
~~~~
39-
!!! error TS2540: Cannot assign to 'bad3' because it is a constant or a read-only property.
39+
!!! error TS2540: Cannot assign to 'bad3' because it is a read-only property.
4040

4141
==== tests/cases/conformance/jsdoc/mod1.js (0 errors) ====
4242
const obj = { value: 42, writable: true };

0 commit comments

Comments
 (0)