Skip to content

Commit e44d39d

Browse files
Updated error message for TS2539 (microsoft#39827)
* Updated error message for TS2539 * Switch to multiple error messages * inline variable Co-authored-by: Nathan Shively-Sanders <[email protected]>
1 parent 9933c8a commit e44d39d

35 files changed

+269
-242
lines changed

src/compiler/checker.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -23728,7 +23728,14 @@ namespace ts {
2372823728
if (assignmentKind) {
2372923729
if (!(localOrExportSymbol.flags & SymbolFlags.Variable) &&
2373023730
!(isInJSFile(node) && localOrExportSymbol.flags & SymbolFlags.ValueModule)) {
23731-
error(node, Diagnostics.Cannot_assign_to_0_because_it_is_not_a_variable, symbolToString(symbol));
23731+
const assignmentError = localOrExportSymbol.flags & SymbolFlags.Enum ? Diagnostics.Cannot_assign_to_0_because_it_is_an_enum
23732+
: localOrExportSymbol.flags & SymbolFlags.Class ? Diagnostics.Cannot_assign_to_0_because_it_is_a_class
23733+
: localOrExportSymbol.flags & SymbolFlags.Module ? Diagnostics.Cannot_assign_to_0_because_it_is_a_namespace
23734+
: localOrExportSymbol.flags & SymbolFlags.Function ? Diagnostics.Cannot_assign_to_0_because_it_is_a_function
23735+
: localOrExportSymbol.flags & SymbolFlags.Alias ? Diagnostics.Cannot_assign_to_0_because_it_is_an_import
23736+
: Diagnostics.Cannot_assign_to_0_because_it_is_not_a_variable;
23737+
23738+
error(node, assignmentError, symbolToString(symbol));
2373223739
return errorType;
2373323740
}
2373423741
if (isReadonlySymbol(localOrExportSymbol)) {

src/compiler/diagnosticMessages.json

+20
Original file line numberDiff line numberDiff line change
@@ -2654,6 +2654,26 @@
26542654
"category": "Error",
26552655
"code": 2627
26562656
},
2657+
"Cannot assign to '{0}' because it is an enum.": {
2658+
"category": "Error",
2659+
"code": 2628
2660+
},
2661+
"Cannot assign to '{0}' because it is a class.": {
2662+
"category": "Error",
2663+
"code": 2629
2664+
},
2665+
"Cannot assign to '{0}' because it is a function.": {
2666+
"category": "Error",
2667+
"code": 2630
2668+
},
2669+
"Cannot assign to '{0}' because it is a namespace.": {
2670+
"category": "Error",
2671+
"code": 2631
2672+
},
2673+
"Cannot assign to '{0}' because it is an import.": {
2674+
"category": "Error",
2675+
"code": 2632
2676+
},
26572677

26582678
"Cannot augment module '{0}' with value exports because it resolves to a non-module entity.": {
26592679
"category": "Error",
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
1-
tests/cases/compiler/arithAssignTyping.ts(3,1): error TS2539: Cannot assign to 'f' because it is not a variable.
2-
tests/cases/compiler/arithAssignTyping.ts(4,1): error TS2539: Cannot assign to 'f' because it is not a variable.
3-
tests/cases/compiler/arithAssignTyping.ts(5,1): error TS2539: Cannot assign to 'f' because it is not a variable.
4-
tests/cases/compiler/arithAssignTyping.ts(6,1): error TS2539: Cannot assign to 'f' because it is not a variable.
5-
tests/cases/compiler/arithAssignTyping.ts(7,1): error TS2539: Cannot assign to 'f' because it is not a variable.
6-
tests/cases/compiler/arithAssignTyping.ts(8,1): error TS2539: Cannot assign to 'f' because it is not a variable.
7-
tests/cases/compiler/arithAssignTyping.ts(9,1): error TS2539: Cannot assign to 'f' because it is not a variable.
8-
tests/cases/compiler/arithAssignTyping.ts(10,1): error TS2539: Cannot assign to 'f' because it is not a variable.
9-
tests/cases/compiler/arithAssignTyping.ts(11,1): error TS2539: Cannot assign to 'f' because it is not a variable.
10-
tests/cases/compiler/arithAssignTyping.ts(12,1): error TS2539: Cannot assign to 'f' because it is not a variable.
11-
tests/cases/compiler/arithAssignTyping.ts(13,1): error TS2539: Cannot assign to 'f' because it is not a variable.
12-
tests/cases/compiler/arithAssignTyping.ts(14,1): error TS2539: Cannot assign to 'f' because it is not a variable.
1+
tests/cases/compiler/arithAssignTyping.ts(3,1): error TS2629: Cannot assign to 'f' because it is a class.
2+
tests/cases/compiler/arithAssignTyping.ts(4,1): error TS2629: Cannot assign to 'f' because it is a class.
3+
tests/cases/compiler/arithAssignTyping.ts(5,1): error TS2629: Cannot assign to 'f' because it is a class.
4+
tests/cases/compiler/arithAssignTyping.ts(6,1): error TS2629: Cannot assign to 'f' because it is a class.
5+
tests/cases/compiler/arithAssignTyping.ts(7,1): error TS2629: Cannot assign to 'f' because it is a class.
6+
tests/cases/compiler/arithAssignTyping.ts(8,1): error TS2629: Cannot assign to 'f' because it is a class.
7+
tests/cases/compiler/arithAssignTyping.ts(9,1): error TS2629: Cannot assign to 'f' because it is a class.
8+
tests/cases/compiler/arithAssignTyping.ts(10,1): error TS2629: Cannot assign to 'f' because it is a class.
9+
tests/cases/compiler/arithAssignTyping.ts(11,1): error TS2629: Cannot assign to 'f' because it is a class.
10+
tests/cases/compiler/arithAssignTyping.ts(12,1): error TS2629: Cannot assign to 'f' because it is a class.
11+
tests/cases/compiler/arithAssignTyping.ts(13,1): error TS2629: Cannot assign to 'f' because it is a class.
12+
tests/cases/compiler/arithAssignTyping.ts(14,1): error TS2629: Cannot assign to 'f' because it is a class.
1313

1414

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

1818
f += ''; // error
1919
~
20-
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
20+
!!! error TS2629: Cannot assign to 'f' because it is a class.
2121
f += 1; // error
2222
~
23-
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
23+
!!! error TS2629: Cannot assign to 'f' because it is a class.
2424
f -= 1; // error
2525
~
26-
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
26+
!!! error TS2629: Cannot assign to 'f' because it is a class.
2727
f *= 1; // error
2828
~
29-
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
29+
!!! error TS2629: Cannot assign to 'f' because it is a class.
3030
f /= 1; // error
3131
~
32-
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
32+
!!! error TS2629: Cannot assign to 'f' because it is a class.
3333
f %= 1; // error
3434
~
35-
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
35+
!!! error TS2629: Cannot assign to 'f' because it is a class.
3636
f &= 1; // error
3737
~
38-
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
38+
!!! error TS2629: Cannot assign to 'f' because it is a class.
3939
f |= 1; // error
4040
~
41-
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
41+
!!! error TS2629: Cannot assign to 'f' because it is a class.
4242
f <<= 1; // error
4343
~
44-
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
44+
!!! error TS2629: Cannot assign to 'f' because it is a class.
4545
f >>= 1; // error
4646
~
47-
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
47+
!!! error TS2629: Cannot assign to 'f' because it is a class.
4848
f >>>= 1; // error
4949
~
50-
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
50+
!!! error TS2629: Cannot assign to 'f' because it is a class.
5151
f ^= 1; // error
5252
~
53-
!!! error TS2539: Cannot assign to 'f' because it is not a variable.
53+
!!! error TS2629: Cannot assign to 'f' because it is a class.

tests/baselines/reference/assignAnyToEveryType.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/conformance/types/any/assignAnyToEveryType.ts(41,1): error TS2539: Cannot assign to 'M' because it is not a variable.
1+
tests/cases/conformance/types/any/assignAnyToEveryType.ts(41,1): error TS2631: Cannot assign to 'M' because it is a namespace.
22

33

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

4545
M = x;
4646
~
47-
!!! error TS2539: Cannot assign to 'M' because it is not a variable.
47+
!!! error TS2631: Cannot assign to 'M' because it is a namespace.
4848

4949
function k<T>(a: T) {
5050
a = x;

tests/baselines/reference/assignToEnum.errors.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tests/cases/compiler/assignToEnum.ts(2,1): error TS2539: Cannot assign to 'A' because it is not a variable.
2-
tests/cases/compiler/assignToEnum.ts(3,1): error TS2539: Cannot assign to 'A' because it is not a variable.
1+
tests/cases/compiler/assignToEnum.ts(2,1): error TS2628: Cannot assign to 'A' because it is an enum.
2+
tests/cases/compiler/assignToEnum.ts(3,1): error TS2628: Cannot assign to 'A' because it is an enum.
33
tests/cases/compiler/assignToEnum.ts(4,3): error TS2540: Cannot assign to 'foo' because it is a read-only property.
44
tests/cases/compiler/assignToEnum.ts(5,3): error TS2540: Cannot assign to 'foo' because it is a read-only property.
55

@@ -8,10 +8,10 @@ tests/cases/compiler/assignToEnum.ts(5,3): error TS2540: Cannot assign to 'foo'
88
enum A { foo, bar }
99
A = undefined; // invalid LHS
1010
~
11-
!!! error TS2539: Cannot assign to 'A' because it is not a variable.
11+
!!! error TS2628: Cannot assign to 'A' because it is an enum.
1212
A = A.bar; // invalid LHS
1313
~
14-
!!! error TS2539: Cannot assign to 'A' because it is not a variable.
14+
!!! error TS2628: Cannot assign to 'A' because it is an enum.
1515
A.foo = 1; // invalid LHS
1616
~~~
1717
!!! error TS2540: Cannot assign to 'foo' because it is a read-only property.

tests/baselines/reference/assignToExistingClass.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/assignToExistingClass.ts(8,13): error TS2539: Cannot assign to 'Mocked' because it is not a variable.
1+
tests/cases/compiler/assignToExistingClass.ts(8,13): error TS2629: Cannot assign to 'Mocked' because it is a class.
22

33

44
==== tests/cases/compiler/assignToExistingClass.ts (1 errors) ====
@@ -11,7 +11,7 @@ tests/cases/compiler/assignToExistingClass.ts(8,13): error TS2539: Cannot assign
1111
willThrowError() {
1212
Mocked = Mocked || function () { // => Error: Invalid left-hand side of assignment expression.
1313
~~~~~~
14-
!!! error TS2539: Cannot assign to 'Mocked' because it is not a variable.
14+
!!! error TS2629: Cannot assign to 'Mocked' because it is a class.
1515
return { myProp: "test" };
1616
};
1717
}

tests/baselines/reference/assignmentLHSIsValue.errors.txt

+16-16
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(7
33
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.
44
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.
55
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.
6-
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(17,1): error TS2539: Cannot assign to 'M' because it is not a variable.
7-
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(19,1): error TS2539: Cannot assign to 'C' because it is not a variable.
8-
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(22,1): error TS2539: Cannot assign to 'E' because it is not a variable.
9-
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(24,1): error TS2539: Cannot assign to 'foo' because it is not a variable.
6+
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(17,1): error TS2631: Cannot assign to 'M' because it is a namespace.
7+
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(19,1): error TS2629: Cannot assign to 'C' because it is a class.
8+
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(22,1): error TS2628: Cannot assign to 'E' because it is an enum.
9+
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(24,1): error TS2630: Cannot assign to 'foo' because it is a function.
1010
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.
1111
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.
1212
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.
@@ -23,10 +23,10 @@ tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(5
2323
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(51,11): error TS1005: ';' expected.
2424
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.
2525
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.
26-
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(58,2): error TS2539: Cannot assign to 'M' because it is not a variable.
27-
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(59,2): error TS2539: Cannot assign to 'C' because it is not a variable.
28-
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(60,2): error TS2539: Cannot assign to 'E' because it is not a variable.
29-
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(61,2): error TS2539: Cannot assign to 'foo' because it is not a variable.
26+
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(58,2): error TS2631: Cannot assign to 'M' because it is a namespace.
27+
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(59,2): error TS2629: Cannot assign to 'C' because it is a class.
28+
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(60,2): error TS2628: Cannot assign to 'E' because it is an enum.
29+
tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(61,2): error TS2630: Cannot assign to 'foo' because it is a function.
3030
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.
3131
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.
3232
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.
@@ -67,20 +67,20 @@ tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(7
6767
module M { export var a; }
6868
M = value;
6969
~
70-
!!! error TS2539: Cannot assign to 'M' because it is not a variable.
70+
!!! error TS2631: Cannot assign to 'M' because it is a namespace.
7171

7272
C = value;
7373
~
74-
!!! error TS2539: Cannot assign to 'C' because it is not a variable.
74+
!!! error TS2629: Cannot assign to 'C' because it is a class.
7575

7676
enum E { }
7777
E = value;
7878
~
79-
!!! error TS2539: Cannot assign to 'E' because it is not a variable.
79+
!!! error TS2628: Cannot assign to 'E' because it is an enum.
8080

8181
foo = value;
8282
~~~
83-
!!! error TS2539: Cannot assign to 'foo' because it is not a variable.
83+
!!! error TS2630: Cannot assign to 'foo' because it is a function.
8484

8585
// literals
8686
null = value;
@@ -148,16 +148,16 @@ tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(7
148148
!!! error TS2364: The left-hand side of an assignment expression must be a variable or a property access.
149149
(M) = value;
150150
~
151-
!!! error TS2539: Cannot assign to 'M' because it is not a variable.
151+
!!! error TS2631: Cannot assign to 'M' because it is a namespace.
152152
(C) = value;
153153
~
154-
!!! error TS2539: Cannot assign to 'C' because it is not a variable.
154+
!!! error TS2629: Cannot assign to 'C' because it is a class.
155155
(E) = value;
156156
~
157-
!!! error TS2539: Cannot assign to 'E' because it is not a variable.
157+
!!! error TS2628: Cannot assign to 'E' because it is an enum.
158158
(foo) = value;
159159
~~~
160-
!!! error TS2539: Cannot assign to 'foo' because it is not a variable.
160+
!!! error TS2630: Cannot assign to 'foo' because it is a function.
161161
(null) = value;
162162
~~~~~~
163163
!!! error TS2364: The left-hand side of an assignment expression must be a variable or a property access.
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
tests/cases/compiler/assignmentToFunction.ts(2,1): error TS2539: Cannot assign to 'fn' because it is not a variable.
2-
tests/cases/compiler/assignmentToFunction.ts(8,9): error TS2539: Cannot assign to 'bar' because it is not a variable.
1+
tests/cases/compiler/assignmentToFunction.ts(2,1): error TS2630: Cannot assign to 'fn' because it is a function.
2+
tests/cases/compiler/assignmentToFunction.ts(8,9): error TS2630: Cannot assign to 'bar' because it is a function.
33

44

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

1111
module foo {
1212
function xyz() {
1313
function bar() {
1414
}
1515
bar = null;
1616
~~~
17-
!!! error TS2539: Cannot assign to 'bar' because it is not a variable.
17+
!!! error TS2630: Cannot assign to 'bar' because it is a function.
1818
}
1919
}

0 commit comments

Comments
 (0)