Skip to content

Commit ac03ba4

Browse files
authored
fix(checker): report error when using bigint as enum key (#61777)
1 parent a591ca3 commit ac03ba4

File tree

6 files changed

+54
-0
lines changed

6 files changed

+54
-0
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ import {
481481
isAssignmentTarget,
482482
isAutoAccessorPropertyDeclaration,
483483
isAwaitExpression,
484+
isBigIntLiteral,
484485
isBinaryExpression,
485486
isBinaryLogicalOperator,
486487
isBindableObjectDefinePropertyCall,
@@ -47541,6 +47542,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4754147542
if (isComputedNonLiteralName(member.name)) {
4754247543
error(member.name, Diagnostics.Computed_property_names_are_not_allowed_in_enums);
4754347544
}
47545+
else if (isBigIntLiteral(member.name)) {
47546+
error(member.name, Diagnostics.An_enum_member_cannot_have_a_numeric_name);
47547+
}
4754447548
else {
4754547549
const text = getTextOfPropertyName(member.name);
4754647550
if (isNumericLiteralName(text) && !isInfinityOrNaNString(text)) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
enumWithBigint.ts(2,3): error TS2452: An enum member cannot have a numeric name.
2+
3+
4+
==== enumWithBigint.ts (1 errors) ====
5+
enum E {
6+
0n = 0,
7+
~~
8+
!!! error TS2452: An enum member cannot have a numeric name.
9+
}
10+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [tests/cases/compiler/enumWithBigint.ts] ////
2+
3+
//// [enumWithBigint.ts]
4+
enum E {
5+
0n = 0,
6+
}
7+
8+
9+
//// [enumWithBigint.js]
10+
var E;
11+
(function (E) {
12+
E[E[0n] = 0] = 0n;
13+
})(E || (E = {}));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [tests/cases/compiler/enumWithBigint.ts] ////
2+
3+
=== enumWithBigint.ts ===
4+
enum E {
5+
>E : Symbol(E, Decl(enumWithBigint.ts, 0, 0))
6+
7+
0n = 0,
8+
>0n : Symbol(E[0n], Decl(enumWithBigint.ts, 0, 8))
9+
}
10+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [tests/cases/compiler/enumWithBigint.ts] ////
2+
3+
=== enumWithBigint.ts ===
4+
enum E {
5+
>E : E
6+
> : ^
7+
8+
0n = 0,
9+
>0n : E.__missing
10+
> : ^^^^^^^^^^^
11+
>0 : 0
12+
> : ^
13+
}
14+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
enum E {
2+
0n = 0,
3+
}

0 commit comments

Comments
 (0)