fix(checker): report error when using bigint as enum key (#61777)

This commit is contained in:
magic-akari 2025-06-06 07:37:47 +08:00 committed by GitHub
parent a591ca3fdc
commit ac03ba4f02
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 54 additions and 0 deletions

View File

@ -481,6 +481,7 @@ import {
isAssignmentTarget,
isAutoAccessorPropertyDeclaration,
isAwaitExpression,
isBigIntLiteral,
isBinaryExpression,
isBinaryLogicalOperator,
isBindableObjectDefinePropertyCall,
@ -47541,6 +47542,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (isComputedNonLiteralName(member.name)) {
error(member.name, Diagnostics.Computed_property_names_are_not_allowed_in_enums);
}
else if (isBigIntLiteral(member.name)) {
error(member.name, Diagnostics.An_enum_member_cannot_have_a_numeric_name);
}
else {
const text = getTextOfPropertyName(member.name);
if (isNumericLiteralName(text) && !isInfinityOrNaNString(text)) {

View File

@ -0,0 +1,10 @@
enumWithBigint.ts(2,3): error TS2452: An enum member cannot have a numeric name.
==== enumWithBigint.ts (1 errors) ====
enum E {
0n = 0,
~~
!!! error TS2452: An enum member cannot have a numeric name.
}

View File

@ -0,0 +1,13 @@
//// [tests/cases/compiler/enumWithBigint.ts] ////
//// [enumWithBigint.ts]
enum E {
0n = 0,
}
//// [enumWithBigint.js]
var E;
(function (E) {
E[E[0n] = 0] = 0n;
})(E || (E = {}));

View File

@ -0,0 +1,10 @@
//// [tests/cases/compiler/enumWithBigint.ts] ////
=== enumWithBigint.ts ===
enum E {
>E : Symbol(E, Decl(enumWithBigint.ts, 0, 0))
0n = 0,
>0n : Symbol(E[0n], Decl(enumWithBigint.ts, 0, 8))
}

View File

@ -0,0 +1,14 @@
//// [tests/cases/compiler/enumWithBigint.ts] ////
=== enumWithBigint.ts ===
enum E {
>E : E
> : ^
0n = 0,
>0n : E.__missing
> : ^^^^^^^^^^^
>0 : 0
> : ^
}

View File

@ -0,0 +1,3 @@
enum E {
0n = 0,
}