Fix BigInt literal error in ambient contexts when targeting < ES2020 (#61935)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
This commit is contained in:
Copilot 2025-06-25 14:29:50 -07:00 committed by GitHub
parent 1ed8674bba
commit 6e519c59c1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 108 additions and 1 deletions

View File

@ -53188,7 +53188,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const literalType = isLiteralTypeNode(node.parent) ||
isPrefixUnaryExpression(node.parent) && isLiteralTypeNode(node.parent.parent);
if (!literalType) {
if (languageVersion < ScriptTarget.ES2020) {
// Don't error on BigInt literals in ambient contexts
if (!(node.flags & NodeFlags.Ambient) && languageVersion < ScriptTarget.ES2020) {
if (grammarErrorOnNode(node, Diagnostics.BigInt_literals_are_not_available_when_targeting_lower_than_ES2020)) {
return true;
}

View File

@ -0,0 +1,17 @@
/main.ts(5,17): error TS2737: BigInt literals are not available when targeting lower than ES2020.
==== /ambient.d.ts (0 errors) ====
declare const fromDts = 789n;
declare namespace Lib {
const value = 999n;
}
==== /main.ts (1 errors) ====
// Minimal repro from issue
declare const n = 123n;
// Non-ambient for comparison
const regular = 456n;
~~~~
!!! error TS2737: BigInt literals are not available when targeting lower than ES2020.

View File

@ -0,0 +1,18 @@
//// [tests/cases/compiler/bigintAmbientMinimal.ts] ////
//// [ambient.d.ts]
declare const fromDts = 789n;
declare namespace Lib {
const value = 999n;
}
//// [main.ts]
// Minimal repro from issue
declare const n = 123n;
// Non-ambient for comparison
const regular = 456n;
//// [main.js]
// Non-ambient for comparison
var regular = 456n;

View File

@ -0,0 +1,22 @@
//// [tests/cases/compiler/bigintAmbientMinimal.ts] ////
=== /ambient.d.ts ===
declare const fromDts = 789n;
>fromDts : Symbol(fromDts, Decl(ambient.d.ts, 0, 13))
declare namespace Lib {
>Lib : Symbol(Lib, Decl(ambient.d.ts, 0, 29))
const value = 999n;
>value : Symbol(value, Decl(ambient.d.ts, 2, 9))
}
=== /main.ts ===
// Minimal repro from issue
declare const n = 123n;
>n : Symbol(n, Decl(main.ts, 1, 13))
// Non-ambient for comparison
const regular = 456n;
>regular : Symbol(regular, Decl(main.ts, 4, 5))

View File

@ -0,0 +1,35 @@
//// [tests/cases/compiler/bigintAmbientMinimal.ts] ////
=== /ambient.d.ts ===
declare const fromDts = 789n;
>fromDts : 789n
> : ^^^^
>789n : 789n
> : ^^^^
declare namespace Lib {
>Lib : typeof Lib
> : ^^^^^^^^^^
const value = 999n;
>value : 999n
> : ^^^^
>999n : 999n
> : ^^^^
}
=== /main.ts ===
// Minimal repro from issue
declare const n = 123n;
>n : 123n
> : ^^^^
>123n : 123n
> : ^^^^
// Non-ambient for comparison
const regular = 456n;
>regular : 456n
> : ^^^^
>456n : 456n
> : ^^^^

View File

@ -0,0 +1,14 @@
// @target: ES5
// @Filename: /ambient.d.ts
declare const fromDts = 789n;
declare namespace Lib {
const value = 999n;
}
// @Filename: /main.ts
// Minimal repro from issue
declare const n = 123n;
// Non-ambient for comparison
const regular = 456n;