From 6e519c59c13e4e99da0bf779a1f2bbee232cf7a1 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Wed, 25 Jun 2025 14:29:50 -0700 Subject: [PATCH] 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> --- src/compiler/checker.ts | 3 +- .../reference/bigintAmbientMinimal.errors.txt | 17 +++++++++ .../reference/bigintAmbientMinimal.js | 18 ++++++++++ .../reference/bigintAmbientMinimal.symbols | 22 ++++++++++++ .../reference/bigintAmbientMinimal.types | 35 +++++++++++++++++++ tests/cases/compiler/bigintAmbientMinimal.ts | 14 ++++++++ 6 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/bigintAmbientMinimal.errors.txt create mode 100644 tests/baselines/reference/bigintAmbientMinimal.js create mode 100644 tests/baselines/reference/bigintAmbientMinimal.symbols create mode 100644 tests/baselines/reference/bigintAmbientMinimal.types create mode 100644 tests/cases/compiler/bigintAmbientMinimal.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5614da35039..e66a209af81 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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; } diff --git a/tests/baselines/reference/bigintAmbientMinimal.errors.txt b/tests/baselines/reference/bigintAmbientMinimal.errors.txt new file mode 100644 index 00000000000..e95b511da01 --- /dev/null +++ b/tests/baselines/reference/bigintAmbientMinimal.errors.txt @@ -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. \ No newline at end of file diff --git a/tests/baselines/reference/bigintAmbientMinimal.js b/tests/baselines/reference/bigintAmbientMinimal.js new file mode 100644 index 00000000000..d2147fb7d30 --- /dev/null +++ b/tests/baselines/reference/bigintAmbientMinimal.js @@ -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; diff --git a/tests/baselines/reference/bigintAmbientMinimal.symbols b/tests/baselines/reference/bigintAmbientMinimal.symbols new file mode 100644 index 00000000000..d438ba12359 --- /dev/null +++ b/tests/baselines/reference/bigintAmbientMinimal.symbols @@ -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)) + diff --git a/tests/baselines/reference/bigintAmbientMinimal.types b/tests/baselines/reference/bigintAmbientMinimal.types new file mode 100644 index 00000000000..086ac1d5676 --- /dev/null +++ b/tests/baselines/reference/bigintAmbientMinimal.types @@ -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 +> : ^^^^ + diff --git a/tests/cases/compiler/bigintAmbientMinimal.ts b/tests/cases/compiler/bigintAmbientMinimal.ts new file mode 100644 index 00000000000..4bad00279bc --- /dev/null +++ b/tests/cases/compiler/bigintAmbientMinimal.ts @@ -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; \ No newline at end of file