Skip to content

Commit 40899ea

Browse files
author
Andy
authored
Error on 'const' in class expression (microsoft#25125)
1 parent 3e50f50 commit 40899ea

6 files changed

Lines changed: 47 additions & 1 deletion

File tree

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27349,7 +27349,7 @@ namespace ts {
2734927349
}
2735027350
switch (modifier.kind) {
2735127351
case SyntaxKind.ConstKeyword:
27352-
if (node.kind !== SyntaxKind.EnumDeclaration && node.parent.kind === SyntaxKind.ClassDeclaration) {
27352+
if (node.kind !== SyntaxKind.EnumDeclaration) {
2735327353
return grammarErrorOnNode(node, Diagnostics.A_class_member_cannot_have_the_0_keyword, tokenToString(SyntaxKind.ConstKeyword));
2735427354
}
2735527355
break;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/compiler/constInClassExpression.ts(2,11): error TS1248: A class member cannot have the 'const' keyword.
2+
3+
4+
==== tests/cases/compiler/constInClassExpression.ts (1 errors) ====
5+
let C = class {
6+
const a = 4;
7+
~
8+
!!! error TS1248: A class member cannot have the 'const' keyword.
9+
};
10+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [constInClassExpression.ts]
2+
let C = class {
3+
const a = 4;
4+
};
5+
6+
7+
//// [constInClassExpression.js]
8+
var C = /** @class */ (function () {
9+
function class_1() {
10+
this.a = 4;
11+
}
12+
return class_1;
13+
}());
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/compiler/constInClassExpression.ts ===
2+
let C = class {
3+
>C : Symbol(C, Decl(constInClassExpression.ts, 0, 3))
4+
5+
const a = 4;
6+
>a : Symbol(C.a, Decl(constInClassExpression.ts, 0, 15))
7+
8+
};
9+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/constInClassExpression.ts ===
2+
let C = class {
3+
>C : typeof C
4+
>class { const a = 4;} : typeof C
5+
6+
const a = 4;
7+
>a : number
8+
>4 : 4
9+
10+
};
11+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
let C = class {
2+
const a = 4;
3+
};

0 commit comments

Comments
 (0)