Skip to content

Feature/28297 make error message at a constant or a read only property more specific #28313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9455,7 +9455,7 @@ namespace ts {
if (accessExpression) {
markPropertyAsReferenced(prop, accessExpression, /*isThisAccess*/ accessExpression.expression.kind === SyntaxKind.ThisKeyword);
if (isAssignmentTarget(accessExpression) && (isReferenceToReadonlyEntity(accessExpression, prop) || isReferenceThroughNamespaceImport(accessExpression))) {
error(accessExpression.argumentExpression, Diagnostics.Cannot_assign_to_0_because_it_is_a_constant_or_a_read_only_property, symbolToString(prop));
error(accessExpression.argumentExpression, Diagnostics.Cannot_assign_to_0_because_it_is_a_read_only_property, symbolToString(prop));
return missingType;
}
if (cacheSymbol) {
Expand Down Expand Up @@ -16115,7 +16115,12 @@ namespace ts {
return errorType;
}
if (isReadonlySymbol(localOrExportSymbol)) {
error(node, Diagnostics.Cannot_assign_to_0_because_it_is_a_constant_or_a_read_only_property, symbolToString(symbol));
if (localOrExportSymbol.flags & SymbolFlags.Variable) {
error(node, Diagnostics.Cannot_assign_to_0_because_it_is_a_constant, symbolToString(symbol));
}
else {
error(node, Diagnostics.Cannot_assign_to_0_because_it_is_a_read_only_property, symbolToString(symbol));
}
return errorType;
}
}
Expand Down Expand Up @@ -18700,7 +18705,7 @@ namespace ts {
checkPropertyAccessibility(node, left.kind === SyntaxKind.SuperKeyword, apparentType, prop);
if (assignmentKind) {
if (isReferenceToReadonlyEntity(<Expression>node, prop) || isReferenceThroughNamespaceImport(<Expression>node)) {
error(right, Diagnostics.Cannot_assign_to_0_because_it_is_a_constant_or_a_read_only_property, idText(right));
error(right, Diagnostics.Cannot_assign_to_0_because_it_is_a_read_only_property, idText(right));
return errorType;
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1944,7 +1944,7 @@
"category": "Error",
"code": 2539
},
"Cannot assign to '{0}' because it is a constant or a read-only property.": {
"Cannot assign to '{0}' because it is a read-only property.": {
"category": "Error",
"code": 2540
},
Expand Down Expand Up @@ -2124,6 +2124,10 @@
"category": "Error",
"code": 2587
},
"Cannot assign to '{0}' because it is a constant.": {
"category": "Error",
"code": 2588
},
"JSX element attributes type '{0}' may not be a union type.": {
"category": "Error",
"code": 2600
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/abstractPropertyNegative.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ tests/cases/compiler/abstractPropertyNegative.ts(13,7): error TS2515: Non-abstra
tests/cases/compiler/abstractPropertyNegative.ts(13,7): error TS2515: Non-abstract class 'C' does not implement inherited abstract member 'readonlyProp' from class 'B'.
tests/cases/compiler/abstractPropertyNegative.ts(15,5): error TS1244: Abstract methods can only appear within an abstract class.
tests/cases/compiler/abstractPropertyNegative.ts(16,37): error TS1005: '{' expected.
tests/cases/compiler/abstractPropertyNegative.ts(19,3): error TS2540: Cannot assign to 'ro' because it is a constant or a read-only property.
tests/cases/compiler/abstractPropertyNegative.ts(19,3): error TS2540: Cannot assign to 'ro' because it is a read-only property.
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'.
Type 'string' is not assignable to type 'number'.
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'.
Expand Down Expand Up @@ -56,7 +56,7 @@ tests/cases/compiler/abstractPropertyNegative.ts(41,18): error TS2676: Accessors
let c = new C();
c.ro = "error: lhs of assignment can't be readonly";
~~
!!! error TS2540: Cannot assign to 'ro' because it is a constant or a read-only property.
!!! error TS2540: Cannot assign to 'ro' because it is a read-only property.

abstract class WrongTypeProperty {
abstract num: number;
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/assignToEnum.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/compiler/assignToEnum.ts(2,1): error TS2539: Cannot assign to 'A' because it is not a variable.
tests/cases/compiler/assignToEnum.ts(3,1): error TS2539: Cannot assign to 'A' because it is not a variable.
tests/cases/compiler/assignToEnum.ts(4,3): error TS2540: Cannot assign to 'foo' because it is a constant or a read-only property.
tests/cases/compiler/assignToEnum.ts(5,3): error TS2540: Cannot assign to 'foo' because it is a constant or a read-only property.
tests/cases/compiler/assignToEnum.ts(4,3): error TS2540: Cannot assign to 'foo' because it is a read-only property.
tests/cases/compiler/assignToEnum.ts(5,3): error TS2540: Cannot assign to 'foo' because it is a read-only property.


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


4 changes: 2 additions & 2 deletions tests/baselines/reference/assignments.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(11,1): error TS2708: Cannot use namespace 'M' as a value.
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(14,1): error TS2539: Cannot assign to 'C' because it is not a variable.
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(17,1): error TS2539: Cannot assign to 'E' because it is not a variable.
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.
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(18,3): error TS2540: Cannot assign to 'A' because it is a read-only property.
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(21,1): error TS2539: Cannot assign to 'fn' because it is not a variable.
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.

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

function fn() { }
fn = null; // Should be error
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/bigintWithLib.errors.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
tests/cases/compiler/bigintWithLib.ts(4,1): error TS2350: Only a void function can be called with the 'new' keyword.
tests/cases/compiler/bigintWithLib.ts(16,33): error TS2345: Argument of type 'number[]' is not assignable to parameter of type 'ArrayBuffer | SharedArrayBuffer'.
Type 'number[]' is missing the following properties from type 'SharedArrayBuffer': byteLength, [Symbol.species], [Symbol.toStringTag]
tests/cases/compiler/bigintWithLib.ts(21,13): error TS2540: Cannot assign to 'length' because it is a constant or a read-only property.
tests/cases/compiler/bigintWithLib.ts(21,13): error TS2540: Cannot assign to 'length' because it is a read-only property.
tests/cases/compiler/bigintWithLib.ts(28,35): error TS2345: Argument of type 'number[]' is not assignable to parameter of type 'ArrayBuffer | SharedArrayBuffer'.
Type 'number[]' is not assignable to type 'SharedArrayBuffer'.
tests/cases/compiler/bigintWithLib.ts(33,13): error TS2540: Cannot assign to 'length' because it is a constant or a read-only property.
tests/cases/compiler/bigintWithLib.ts(33,13): error TS2540: Cannot assign to 'length' because it is a read-only property.
tests/cases/compiler/bigintWithLib.ts(40,25): error TS2345: Argument of type '-1' is not assignable to parameter of type 'bigint'.
tests/cases/compiler/bigintWithLib.ts(43,26): error TS2345: Argument of type '123' is not assignable to parameter of type 'bigint'.

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

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

// Test added DataView methods
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
tests/cases/conformance/jsdoc/validator.ts(17,4): error TS2540: Cannot assign to 'readonlyProp' because it is a constant or a read-only property.
tests/cases/conformance/jsdoc/validator.ts(18,4): error TS2540: Cannot assign to 'readonlyAccessor' because it is a constant or a read-only property.
tests/cases/conformance/jsdoc/validator.ts(17,4): error TS2540: Cannot assign to 'readonlyProp' because it is a read-only property.
tests/cases/conformance/jsdoc/validator.ts(18,4): error TS2540: Cannot assign to 'readonlyAccessor' because it is a read-only property.
tests/cases/conformance/jsdoc/validator.ts(19,1): error TS2322: Type '"no"' is not assignable to type 'number'.
tests/cases/conformance/jsdoc/validator.ts(20,1): error TS2322: Type '"no"' is not assignable to type 'number'.
tests/cases/conformance/jsdoc/validator.ts(21,1): error TS2322: Type '0' is not assignable to type 'string'.
tests/cases/conformance/jsdoc/validator.ts(37,4): error TS2540: Cannot assign to 'readonlyProp' because it is a constant or a read-only property.
tests/cases/conformance/jsdoc/validator.ts(38,4): error TS2540: Cannot assign to 'readonlyAccessor' because it is a constant or a read-only property.
tests/cases/conformance/jsdoc/validator.ts(37,4): error TS2540: Cannot assign to 'readonlyProp' because it is a read-only property.
tests/cases/conformance/jsdoc/validator.ts(38,4): error TS2540: Cannot assign to 'readonlyAccessor' because it is a read-only property.
tests/cases/conformance/jsdoc/validator.ts(39,1): error TS2322: Type '0' is not assignable to type 'string'.
tests/cases/conformance/jsdoc/validator.ts(40,1): error TS2322: Type '"no"' is not assignable to type 'number'.
tests/cases/conformance/jsdoc/validator.ts(41,1): error TS2322: Type '0' is not assignable to type 'string'.
Expand All @@ -29,10 +29,10 @@ tests/cases/conformance/jsdoc/validator.ts(41,1): error TS2322: Type '0' is not
// disallowed assignments
m1.readonlyProp = "name";
~~~~~~~~~~~~
!!! error TS2540: Cannot assign to 'readonlyProp' because it is a constant or a read-only property.
!!! error TS2540: Cannot assign to 'readonlyProp' because it is a read-only property.
m1.readonlyAccessor = 12;
~~~~~~~~~~~~~~~~
!!! error TS2540: Cannot assign to 'readonlyAccessor' because it is a constant or a read-only property.
!!! error TS2540: Cannot assign to 'readonlyAccessor' because it is a read-only property.
m1.thing = "no";
~~~~~~~~
!!! error TS2322: Type '"no"' is not assignable to type 'number'.
Expand All @@ -59,10 +59,10 @@ tests/cases/conformance/jsdoc/validator.ts(41,1): error TS2322: Type '0' is not
// disallowed assignments
m2.readonlyProp = "name";
~~~~~~~~~~~~
!!! error TS2540: Cannot assign to 'readonlyProp' because it is a constant or a read-only property.
!!! error TS2540: Cannot assign to 'readonlyProp' because it is a read-only property.
m2.readonlyAccessor = 12;
~~~~~~~~~~~~~~~~
!!! error TS2540: Cannot assign to 'readonlyAccessor' because it is a constant or a read-only property.
!!! error TS2540: Cannot assign to 'readonlyAccessor' because it is a read-only property.
m2.thing = 0;
~~~~~~~~
!!! error TS2322: Type '0' is not assignable to type 'string'.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/conformance/jsdoc/validator.ts(19,4): error TS2540: Cannot assign to 'readonlyProp' because it is a constant or a read-only property.
tests/cases/conformance/jsdoc/validator.ts(20,4): error TS2540: Cannot assign to 'readonlyAccessor' because it is a constant or a read-only property.
tests/cases/conformance/jsdoc/validator.ts(19,4): error TS2540: Cannot assign to 'readonlyProp' because it is a read-only property.
tests/cases/conformance/jsdoc/validator.ts(20,4): error TS2540: Cannot assign to 'readonlyAccessor' because it is a read-only property.
tests/cases/conformance/jsdoc/validator.ts(21,1): error TS2322: Type '"no"' is not assignable to type 'number'.
tests/cases/conformance/jsdoc/validator.ts(22,1): error TS2322: Type '"no"' is not assignable to type 'number'.
tests/cases/conformance/jsdoc/validator.ts(23,1): error TS2322: Type '0' is not assignable to type 'string'.
Expand All @@ -26,10 +26,10 @@ tests/cases/conformance/jsdoc/validator.ts(23,1): error TS2322: Type '0' is not
// disallowed assignments
m1.readonlyProp = "name";
~~~~~~~~~~~~
!!! error TS2540: Cannot assign to 'readonlyProp' because it is a constant or a read-only property.
!!! error TS2540: Cannot assign to 'readonlyProp' because it is a read-only property.
m1.readonlyAccessor = 12;
~~~~~~~~~~~~~~~~
!!! error TS2540: Cannot assign to 'readonlyAccessor' because it is a constant or a read-only property.
!!! error TS2540: Cannot assign to 'readonlyAccessor' because it is a read-only property.
m1.thing = "no";
~~~~~~~~
!!! error TS2322: Type '"no"' is not assignable to type 'number'.
Expand Down
12 changes: 6 additions & 6 deletions tests/baselines/reference/checkObjectDefineProperty.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/conformance/jsdoc/validate.ts(14,3): error TS2540: Cannot assign to 'lastName' because it is a constant or a read-only property.
tests/cases/conformance/jsdoc/validate.ts(15,3): error TS2540: Cannot assign to 'houseNumber' because it is a constant or a read-only property.
tests/cases/conformance/jsdoc/validate.ts(14,3): error TS2540: Cannot assign to 'lastName' because it is a read-only property.
tests/cases/conformance/jsdoc/validate.ts(15,3): error TS2540: Cannot assign to 'houseNumber' because it is a read-only property.
tests/cases/conformance/jsdoc/validate.ts(16,1): error TS2322: Type '12' is not assignable to type 'string'.
tests/cases/conformance/jsdoc/validate.ts(17,3): error TS2540: Cannot assign to 'middleInit' because it is a constant or a read-only property.
tests/cases/conformance/jsdoc/validate.ts(17,3): error TS2540: Cannot assign to 'middleInit' because it is a read-only property.


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

x.lastName = "should fail";
~~~~~~~~
!!! error TS2540: Cannot assign to 'lastName' because it is a constant or a read-only property.
!!! error TS2540: Cannot assign to 'lastName' because it is a read-only property.
x.houseNumber = 12; // should also fail
~~~~~~~~~~~
!!! error TS2540: Cannot assign to 'houseNumber' because it is a constant or a read-only property.
!!! error TS2540: Cannot assign to 'houseNumber' because it is a read-only property.
x.zipStr = 12; // should fail
~~~~~~~~
!!! error TS2322: Type '12' is not assignable to type 'string'.
x.middleInit = "R"; // should also fail
~~~~~~~~~~
!!! error TS2540: Cannot assign to 'middleInit' because it is a constant or a read-only property.
!!! error TS2540: Cannot assign to 'middleInit' because it is a read-only property.

==== tests/cases/conformance/jsdoc/index.js (0 errors) ====
const x = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ tests/cases/conformance/jsdoc/importer.js(3,5): error TS2339: Property 'other' d
tests/cases/conformance/jsdoc/importer.js(4,5): error TS2339: Property 'prop' does not exist on type 'typeof import("tests/cases/conformance/jsdoc/mod1")'.
tests/cases/conformance/jsdoc/importer.js(11,5): error TS2339: Property 'other' does not exist on type 'typeof import("tests/cases/conformance/jsdoc/mod1")'.
tests/cases/conformance/jsdoc/importer.js(12,5): error TS2339: Property 'prop' does not exist on type 'typeof import("tests/cases/conformance/jsdoc/mod1")'.
tests/cases/conformance/jsdoc/importer.js(13,5): error TS2540: Cannot assign to 'bad1' because it is a constant or a read-only property.
tests/cases/conformance/jsdoc/importer.js(14,5): error TS2540: Cannot assign to 'bad2' because it is a constant or a read-only property.
tests/cases/conformance/jsdoc/importer.js(15,5): error TS2540: Cannot assign to 'bad3' because it is a constant or a read-only property.
tests/cases/conformance/jsdoc/importer.js(13,5): error TS2540: Cannot assign to 'bad1' because it is a read-only property.
tests/cases/conformance/jsdoc/importer.js(14,5): error TS2540: Cannot assign to 'bad2' because it is a read-only property.
tests/cases/conformance/jsdoc/importer.js(15,5): error TS2540: Cannot assign to 'bad3' because it is a read-only property.


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

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