Skip to content

Commit 6e519c5

Browse files
Fix BigInt literal error in ambient contexts when targeting < ES2020 (#61935)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: RyanCavanaugh <[email protected]>
1 parent 1ed8674 commit 6e519c5

File tree

6 files changed

+108
-1
lines changed

6 files changed

+108
-1
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53188,7 +53188,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
5318853188
const literalType = isLiteralTypeNode(node.parent) ||
5318953189
isPrefixUnaryExpression(node.parent) && isLiteralTypeNode(node.parent.parent);
5319053190
if (!literalType) {
53191-
if (languageVersion < ScriptTarget.ES2020) {
53191+
// Don't error on BigInt literals in ambient contexts
53192+
if (!(node.flags & NodeFlags.Ambient) && languageVersion < ScriptTarget.ES2020) {
5319253193
if (grammarErrorOnNode(node, Diagnostics.BigInt_literals_are_not_available_when_targeting_lower_than_ES2020)) {
5319353194
return true;
5319453195
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/main.ts(5,17): error TS2737: BigInt literals are not available when targeting lower than ES2020.
2+
3+
4+
==== /ambient.d.ts (0 errors) ====
5+
declare const fromDts = 789n;
6+
declare namespace Lib {
7+
const value = 999n;
8+
}
9+
10+
==== /main.ts (1 errors) ====
11+
// Minimal repro from issue
12+
declare const n = 123n;
13+
14+
// Non-ambient for comparison
15+
const regular = 456n;
16+
~~~~
17+
!!! error TS2737: BigInt literals are not available when targeting lower than ES2020.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [tests/cases/compiler/bigintAmbientMinimal.ts] ////
2+
3+
//// [ambient.d.ts]
4+
declare const fromDts = 789n;
5+
declare namespace Lib {
6+
const value = 999n;
7+
}
8+
9+
//// [main.ts]
10+
// Minimal repro from issue
11+
declare const n = 123n;
12+
13+
// Non-ambient for comparison
14+
const regular = 456n;
15+
16+
//// [main.js]
17+
// Non-ambient for comparison
18+
var regular = 456n;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [tests/cases/compiler/bigintAmbientMinimal.ts] ////
2+
3+
=== /ambient.d.ts ===
4+
declare const fromDts = 789n;
5+
>fromDts : Symbol(fromDts, Decl(ambient.d.ts, 0, 13))
6+
7+
declare namespace Lib {
8+
>Lib : Symbol(Lib, Decl(ambient.d.ts, 0, 29))
9+
10+
const value = 999n;
11+
>value : Symbol(value, Decl(ambient.d.ts, 2, 9))
12+
}
13+
14+
=== /main.ts ===
15+
// Minimal repro from issue
16+
declare const n = 123n;
17+
>n : Symbol(n, Decl(main.ts, 1, 13))
18+
19+
// Non-ambient for comparison
20+
const regular = 456n;
21+
>regular : Symbol(regular, Decl(main.ts, 4, 5))
22+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//// [tests/cases/compiler/bigintAmbientMinimal.ts] ////
2+
3+
=== /ambient.d.ts ===
4+
declare const fromDts = 789n;
5+
>fromDts : 789n
6+
> : ^^^^
7+
>789n : 789n
8+
> : ^^^^
9+
10+
declare namespace Lib {
11+
>Lib : typeof Lib
12+
> : ^^^^^^^^^^
13+
14+
const value = 999n;
15+
>value : 999n
16+
> : ^^^^
17+
>999n : 999n
18+
> : ^^^^
19+
}
20+
21+
=== /main.ts ===
22+
// Minimal repro from issue
23+
declare const n = 123n;
24+
>n : 123n
25+
> : ^^^^
26+
>123n : 123n
27+
> : ^^^^
28+
29+
// Non-ambient for comparison
30+
const regular = 456n;
31+
>regular : 456n
32+
> : ^^^^
33+
>456n : 456n
34+
> : ^^^^
35+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @target: ES5
2+
3+
// @Filename: /ambient.d.ts
4+
declare const fromDts = 789n;
5+
declare namespace Lib {
6+
const value = 999n;
7+
}
8+
9+
// @Filename: /main.ts
10+
// Minimal repro from issue
11+
declare const n = 123n;
12+
13+
// Non-ambient for comparison
14+
const regular = 456n;

0 commit comments

Comments
 (0)