Skip to content

Commit bb99c41

Browse files
committed
Don't error on bigint literal used in type
1 parent 6c59a3b commit bb99c41

6 files changed

+34
-21
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30471,13 +30471,17 @@ namespace ts {
3047130471
}
3047230472

3047330473
function checkGrammarBigIntLiteral(node: BigIntLiteral): boolean {
30474-
if (languageVersion < ScriptTarget.ESNext) {
30475-
if (grammarErrorOnNode(node, Diagnostics.BigInt_literals_are_not_available_when_targetting_lower_than_ESNext)) {
30476-
return true;
30474+
const literalType = isLiteralTypeNode(node.parent) ||
30475+
isPrefixUnaryExpression(node.parent) && isLiteralTypeNode(node.parent.parent);
30476+
if (!literalType) {
30477+
if (languageVersion < ScriptTarget.ESNext) {
30478+
if (grammarErrorOnNode(node, Diagnostics.BigInt_literals_are_not_available_when_targetting_lower_than_ESNext)) {
30479+
return true;
30480+
}
30481+
}
30482+
if (!compilerOptions.experimentalBigInt) {
30483+
return grammarErrorOnNode(node, Diagnostics.Experimental_support_for_BigInt_is_a_feature_that_is_subject_to_change_in_a_future_release_Set_the_experimentalBigInt_option_to_remove_this_warning);
3047730484
}
30478-
}
30479-
if (!compilerOptions.experimentalBigInt) {
30480-
return grammarErrorOnNode(node, Diagnostics.Experimental_support_for_BigInt_is_a_feature_that_is_subject_to_change_in_a_future_release_Set_the_experimentalBigInt_option_to_remove_this_warning);
3048130485
}
3048230486
return false;
3048330487
}

tests/baselines/reference/warnExperimentalBigIntLiteral.errors.txt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
tests/cases/compiler/warnExperimentalBigIntLiteral.ts(3,24): error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning.
2-
tests/cases/compiler/warnExperimentalBigIntLiteral.ts(4,22): error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning.
3-
tests/cases/compiler/warnExperimentalBigIntLiteral.ts(4,29): error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning.
4-
tests/cases/compiler/warnExperimentalBigIntLiteral.ts(4,39): error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning.
5-
tests/cases/compiler/warnExperimentalBigIntLiteral.ts(4,48): error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning.
1+
tests/cases/compiler/warnExperimentalBigIntLiteral.ts(5,22): error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning.
2+
tests/cases/compiler/warnExperimentalBigIntLiteral.ts(5,29): error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning.
3+
tests/cases/compiler/warnExperimentalBigIntLiteral.ts(5,39): error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning.
4+
tests/cases/compiler/warnExperimentalBigIntLiteral.ts(5,48): error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning.
65

76

8-
==== tests/cases/compiler/warnExperimentalBigIntLiteral.ts (5 errors) ====
7+
==== tests/cases/compiler/warnExperimentalBigIntLiteral.ts (4 errors) ====
98
const normalNumber = 123; // should not error
109
let bigintType: bigint; // should not error
11-
let bigintLiteralType: 123n; // should error when used as type
12-
~~~~
13-
!!! error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning.
10+
let bigintLiteralType: 123n; // should not error when used as type
11+
let bigintNegativeLiteralType: -123n; // should not error when used as type
1412
const bigintNumber = 123n * 0b1111n + 0o444n * 0x7fn; // each literal should error
1513
~~~~
1614
!!! error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning.
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
//// [warnExperimentalBigIntLiteral.ts]
22
const normalNumber = 123; // should not error
33
let bigintType: bigint; // should not error
4-
let bigintLiteralType: 123n; // should error when used as type
4+
let bigintLiteralType: 123n; // should not error when used as type
5+
let bigintNegativeLiteralType: -123n; // should not error when used as type
56
const bigintNumber = 123n * 0b1111n + 0o444n * 0x7fn; // each literal should error
67

78
//// [warnExperimentalBigIntLiteral.js]
89
const normalNumber = 123; // should not error
910
let bigintType; // should not error
10-
let bigintLiteralType; // should error when used as type
11+
let bigintLiteralType; // should not error when used as type
12+
let bigintNegativeLiteralType; // should not error when used as type
1113
const bigintNumber = 123n * 15n + 292n * 0x7fn; // each literal should error

tests/baselines/reference/warnExperimentalBigIntLiteral.symbols

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ const normalNumber = 123; // should not error
55
let bigintType: bigint; // should not error
66
>bigintType : Symbol(bigintType, Decl(warnExperimentalBigIntLiteral.ts, 1, 3))
77

8-
let bigintLiteralType: 123n; // should error when used as type
8+
let bigintLiteralType: 123n; // should not error when used as type
99
>bigintLiteralType : Symbol(bigintLiteralType, Decl(warnExperimentalBigIntLiteral.ts, 2, 3))
1010

11+
let bigintNegativeLiteralType: -123n; // should not error when used as type
12+
>bigintNegativeLiteralType : Symbol(bigintNegativeLiteralType, Decl(warnExperimentalBigIntLiteral.ts, 3, 3))
13+
1114
const bigintNumber = 123n * 0b1111n + 0o444n * 0x7fn; // each literal should error
12-
>bigintNumber : Symbol(bigintNumber, Decl(warnExperimentalBigIntLiteral.ts, 3, 5))
15+
>bigintNumber : Symbol(bigintNumber, Decl(warnExperimentalBigIntLiteral.ts, 4, 5))
1316

tests/baselines/reference/warnExperimentalBigIntLiteral.types

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ const normalNumber = 123; // should not error
66
let bigintType: bigint; // should not error
77
>bigintType : bigint
88

9-
let bigintLiteralType: 123n; // should error when used as type
9+
let bigintLiteralType: 123n; // should not error when used as type
1010
>bigintLiteralType : 123n
1111

12+
let bigintNegativeLiteralType: -123n; // should not error when used as type
13+
>bigintNegativeLiteralType : -123n
14+
>-123n : -123n
15+
>123n : 123n
16+
1217
const bigintNumber = 123n * 0b1111n + 0o444n * 0x7fn; // each literal should error
1318
>bigintNumber : bigint
1419
>123n * 0b1111n + 0o444n * 0x7fn : bigint

tests/cases/compiler/warnExperimentalBigIntLiteral.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
const normalNumber = 123; // should not error
44
let bigintType: bigint; // should not error
5-
let bigintLiteralType: 123n; // should error when used as type
5+
let bigintLiteralType: 123n; // should not error when used as type
6+
let bigintNegativeLiteralType: -123n; // should not error when used as type
67
const bigintNumber = 123n * 0b1111n + 0o444n * 0x7fn; // each literal should error

0 commit comments

Comments
 (0)