Skip to content

Updated error message for TS2539 #39827

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
merged 4 commits into from
Mar 11, 2021
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
9 changes: 8 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23691,7 +23691,14 @@ namespace ts {
if (assignmentKind) {
if (!(localOrExportSymbol.flags & SymbolFlags.Variable) &&
!(isInJSFile(node) && localOrExportSymbol.flags & SymbolFlags.ValueModule)) {
error(node, Diagnostics.Cannot_assign_to_0_because_it_is_not_a_variable, symbolToString(symbol));
const assignmentError = localOrExportSymbol.flags & SymbolFlags.Enum ? Diagnostics.Cannot_assign_to_0_because_it_is_an_enum
: localOrExportSymbol.flags & SymbolFlags.Class ? Diagnostics.Cannot_assign_to_0_because_it_is_a_class
: localOrExportSymbol.flags & SymbolFlags.Module ? Diagnostics.Cannot_assign_to_0_because_it_is_a_namespace
: localOrExportSymbol.flags & SymbolFlags.Function ? Diagnostics.Cannot_assign_to_0_because_it_is_a_function
: localOrExportSymbol.flags & SymbolFlags.Alias ? Diagnostics.Cannot_assign_to_0_because_it_is_an_import
: Diagnostics.Cannot_assign_to_0_because_it_is_not_a_variable;

error(node, assignmentError, symbolToString(symbol));
return errorType;
}
if (isReadonlySymbol(localOrExportSymbol)) {
Expand Down
20 changes: 20 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2654,6 +2654,26 @@
"category": "Error",
"code": 2627
},
"Cannot assign to '{0}' because it is an enum.": {
"category": "Error",
"code": 2628
},
"Cannot assign to '{0}' because it is a class.": {
"category": "Error",
"code": 2629
},
"Cannot assign to '{0}' because it is a function.": {
"category": "Error",
"code": 2630
},
"Cannot assign to '{0}' because it is a namespace.": {
"category": "Error",
"code": 2631
},
"Cannot assign to '{0}' because it is an import.": {
"category": "Error",
"code": 2632
},

"Cannot augment module '{0}' with value exports because it resolves to a non-module entity.": {
"category": "Error",
Expand Down
48 changes: 24 additions & 24 deletions tests/baselines/reference/arithAssignTyping.errors.txt
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
tests/cases/compiler/arithAssignTyping.ts(3,1): error TS2539: Cannot assign to 'f' because it is not a variable.
tests/cases/compiler/arithAssignTyping.ts(4,1): error TS2539: Cannot assign to 'f' because it is not a variable.
tests/cases/compiler/arithAssignTyping.ts(5,1): error TS2539: Cannot assign to 'f' because it is not a variable.
tests/cases/compiler/arithAssignTyping.ts(6,1): error TS2539: Cannot assign to 'f' because it is not a variable.
tests/cases/compiler/arithAssignTyping.ts(7,1): error TS2539: Cannot assign to 'f' because it is not a variable.
tests/cases/compiler/arithAssignTyping.ts(8,1): error TS2539: Cannot assign to 'f' because it is not a variable.
tests/cases/compiler/arithAssignTyping.ts(9,1): error TS2539: Cannot assign to 'f' because it is not a variable.
tests/cases/compiler/arithAssignTyping.ts(10,1): error TS2539: Cannot assign to 'f' because it is not a variable.
tests/cases/compiler/arithAssignTyping.ts(11,1): error TS2539: Cannot assign to 'f' because it is not a variable.
tests/cases/compiler/arithAssignTyping.ts(12,1): error TS2539: Cannot assign to 'f' because it is not a variable.
tests/cases/compiler/arithAssignTyping.ts(13,1): error TS2539: Cannot assign to 'f' because it is not a variable.
tests/cases/compiler/arithAssignTyping.ts(14,1): error TS2539: Cannot assign to 'f' because it is not a variable.
tests/cases/compiler/arithAssignTyping.ts(3,1): error TS2629: Cannot assign to 'f' because it is a class.
tests/cases/compiler/arithAssignTyping.ts(4,1): error TS2629: Cannot assign to 'f' because it is a class.
tests/cases/compiler/arithAssignTyping.ts(5,1): error TS2629: Cannot assign to 'f' because it is a class.
tests/cases/compiler/arithAssignTyping.ts(6,1): error TS2629: Cannot assign to 'f' because it is a class.
tests/cases/compiler/arithAssignTyping.ts(7,1): error TS2629: Cannot assign to 'f' because it is a class.
tests/cases/compiler/arithAssignTyping.ts(8,1): error TS2629: Cannot assign to 'f' because it is a class.
tests/cases/compiler/arithAssignTyping.ts(9,1): error TS2629: Cannot assign to 'f' because it is a class.
tests/cases/compiler/arithAssignTyping.ts(10,1): error TS2629: Cannot assign to 'f' because it is a class.
tests/cases/compiler/arithAssignTyping.ts(11,1): error TS2629: Cannot assign to 'f' because it is a class.
tests/cases/compiler/arithAssignTyping.ts(12,1): error TS2629: Cannot assign to 'f' because it is a class.
tests/cases/compiler/arithAssignTyping.ts(13,1): error TS2629: Cannot assign to 'f' because it is a class.
tests/cases/compiler/arithAssignTyping.ts(14,1): error TS2629: Cannot assign to 'f' because it is a class.


==== tests/cases/compiler/arithAssignTyping.ts (12 errors) ====
class f { }

f += ''; // error
~
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
!!! error TS2629: Cannot assign to 'f' because it is a class.
f += 1; // error
~
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
!!! error TS2629: Cannot assign to 'f' because it is a class.
f -= 1; // error
~
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
!!! error TS2629: Cannot assign to 'f' because it is a class.
f *= 1; // error
~
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
!!! error TS2629: Cannot assign to 'f' because it is a class.
f /= 1; // error
~
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
!!! error TS2629: Cannot assign to 'f' because it is a class.
f %= 1; // error
~
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
!!! error TS2629: Cannot assign to 'f' because it is a class.
f &= 1; // error
~
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
!!! error TS2629: Cannot assign to 'f' because it is a class.
f |= 1; // error
~
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
!!! error TS2629: Cannot assign to 'f' because it is a class.
f <<= 1; // error
~
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
!!! error TS2629: Cannot assign to 'f' because it is a class.
f >>= 1; // error
~
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
!!! error TS2629: Cannot assign to 'f' because it is a class.
f >>>= 1; // error
~
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
!!! error TS2629: Cannot assign to 'f' because it is a class.
f ^= 1; // error
~
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
!!! error TS2629: Cannot assign to 'f' because it is a class.
4 changes: 2 additions & 2 deletions tests/baselines/reference/assignAnyToEveryType.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/conformance/types/any/assignAnyToEveryType.ts(41,1): error TS2539: Cannot assign to 'M' because it is not a variable.
tests/cases/conformance/types/any/assignAnyToEveryType.ts(41,1): error TS2631: Cannot assign to 'M' because it is a namespace.


==== tests/cases/conformance/types/any/assignAnyToEveryType.ts (1 errors) ====
Expand Down Expand Up @@ -44,7 +44,7 @@ tests/cases/conformance/types/any/assignAnyToEveryType.ts(41,1): error TS2539: C

M = x;
~
!!! error TS2539: Cannot assign to 'M' because it is not a variable.
!!! error TS2631: Cannot assign to 'M' because it is a namespace.

function k<T>(a: T) {
a = x;
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,5 +1,5 @@
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(2,1): error TS2628: Cannot assign to 'A' because it is an enum.
tests/cases/compiler/assignToEnum.ts(3,1): error TS2628: Cannot assign to 'A' because it is an enum.
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.

Expand All @@ -8,10 +8,10 @@ tests/cases/compiler/assignToEnum.ts(5,3): error TS2540: Cannot assign to 'foo'
enum A { foo, bar }
A = undefined; // invalid LHS
~
!!! error TS2539: Cannot assign to 'A' because it is not a variable.
!!! error TS2628: Cannot assign to 'A' because it is an enum.
A = A.bar; // invalid LHS
~
!!! error TS2539: Cannot assign to 'A' because it is not a variable.
!!! error TS2628: Cannot assign to 'A' because it is an enum.
A.foo = 1; // invalid LHS
~~~
!!! error TS2540: Cannot assign to 'foo' because it is a read-only property.
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/assignToExistingClass.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/compiler/assignToExistingClass.ts(8,13): error TS2539: Cannot assign to 'Mocked' because it is not a variable.
tests/cases/compiler/assignToExistingClass.ts(8,13): error TS2629: Cannot assign to 'Mocked' because it is a class.


==== tests/cases/compiler/assignToExistingClass.ts (1 errors) ====
Expand All @@ -11,7 +11,7 @@ tests/cases/compiler/assignToExistingClass.ts(8,13): error TS2539: Cannot assign
willThrowError() {
Mocked = Mocked || function () { // => Error: Invalid left-hand side of assignment expression.
~~~~~~
!!! error TS2539: Cannot assign to 'Mocked' because it is not a variable.
!!! error TS2629: Cannot assign to 'Mocked' because it is a class.
return { myProp: "test" };
};
}
Expand Down
32 changes: 16 additions & 16 deletions tests/baselines/reference/assignmentLHSIsValue.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(7
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(8,21): error TS2364: The left-hand side of an assignment expression must be a variable or a property access.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(11,18): error TS2364: The left-hand side of an assignment expression must be a variable or a property access.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(13,1): error TS2364: The left-hand side of an assignment expression must be a variable or a property access.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(17,1): error TS2539: Cannot assign to 'M' because it is not a variable.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(19,1): error TS2539: Cannot assign to 'C' because it is not a variable.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(22,1): error TS2539: Cannot assign to 'E' because it is not a variable.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(24,1): error TS2539: Cannot assign to 'foo' because it is not a variable.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(17,1): error TS2631: Cannot assign to 'M' because it is a namespace.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(19,1): error TS2629: Cannot assign to 'C' because it is a class.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(22,1): error TS2628: Cannot assign to 'E' because it is an enum.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(24,1): error TS2630: Cannot assign to 'foo' because it is a function.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(27,1): error TS2364: The left-hand side of an assignment expression must be a variable or a property access.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(28,1): error TS2364: The left-hand side of an assignment expression must be a variable or a property access.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(29,1): error TS2364: The left-hand side of an assignment expression must be a variable or a property access.
Expand All @@ -23,10 +23,10 @@ tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(5
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(51,11): error TS1005: ';' expected.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(54,1): error TS2364: The left-hand side of an assignment expression must be a variable or a property access.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(57,1): error TS2364: The left-hand side of an assignment expression must be a variable or a property access.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(58,2): error TS2539: Cannot assign to 'M' because it is not a variable.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(59,2): error TS2539: Cannot assign to 'C' because it is not a variable.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(60,2): error TS2539: Cannot assign to 'E' because it is not a variable.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(61,2): error TS2539: Cannot assign to 'foo' because it is not a variable.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(58,2): error TS2631: Cannot assign to 'M' because it is a namespace.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(59,2): error TS2629: Cannot assign to 'C' because it is a class.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(60,2): error TS2628: Cannot assign to 'E' because it is an enum.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(61,2): error TS2630: Cannot assign to 'foo' because it is a function.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(62,1): error TS2364: The left-hand side of an assignment expression must be a variable or a property access.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(63,1): error TS2364: The left-hand side of an assignment expression must be a variable or a property access.
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(64,1): error TS2364: The left-hand side of an assignment expression must be a variable or a property access.
Expand Down Expand Up @@ -67,20 +67,20 @@ tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(7
module M { export var a; }
M = value;
~
!!! error TS2539: Cannot assign to 'M' because it is not a variable.
!!! error TS2631: Cannot assign to 'M' because it is a namespace.

C = value;
~
!!! error TS2539: Cannot assign to 'C' because it is not a variable.
!!! error TS2629: Cannot assign to 'C' because it is a class.

enum E { }
E = value;
~
!!! error TS2539: Cannot assign to 'E' because it is not a variable.
!!! error TS2628: Cannot assign to 'E' because it is an enum.

foo = value;
~~~
!!! error TS2539: Cannot assign to 'foo' because it is not a variable.
!!! error TS2630: Cannot assign to 'foo' because it is a function.

// literals
null = value;
Expand Down Expand Up @@ -148,16 +148,16 @@ tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(7
!!! error TS2364: The left-hand side of an assignment expression must be a variable or a property access.
(M) = value;
~
!!! error TS2539: Cannot assign to 'M' because it is not a variable.
!!! error TS2631: Cannot assign to 'M' because it is a namespace.
(C) = value;
~
!!! error TS2539: Cannot assign to 'C' because it is not a variable.
!!! error TS2629: Cannot assign to 'C' because it is a class.
(E) = value;
~
!!! error TS2539: Cannot assign to 'E' because it is not a variable.
!!! error TS2628: Cannot assign to 'E' because it is an enum.
(foo) = value;
~~~
!!! error TS2539: Cannot assign to 'foo' because it is not a variable.
!!! error TS2630: Cannot assign to 'foo' because it is a function.
(null) = value;
~~~~~~
!!! error TS2364: The left-hand side of an assignment expression must be a variable or a property access.
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/assignmentToFunction.errors.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
tests/cases/compiler/assignmentToFunction.ts(2,1): error TS2539: Cannot assign to 'fn' because it is not a variable.
tests/cases/compiler/assignmentToFunction.ts(8,9): error TS2539: Cannot assign to 'bar' because it is not a variable.
tests/cases/compiler/assignmentToFunction.ts(2,1): error TS2630: Cannot assign to 'fn' because it is a function.
tests/cases/compiler/assignmentToFunction.ts(8,9): error TS2630: Cannot assign to 'bar' because it is a function.


==== tests/cases/compiler/assignmentToFunction.ts (2 errors) ====
function fn() { }
fn = () => 3;
~~
!!! error TS2539: Cannot assign to 'fn' because it is not a variable.
!!! error TS2630: Cannot assign to 'fn' because it is a function.

module foo {
function xyz() {
function bar() {
}
bar = null;
~~~
!!! error TS2539: Cannot assign to 'bar' because it is not a variable.
!!! error TS2630: Cannot assign to 'bar' because it is a function.
}
}
Loading