diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a16cab1443c..308f3a61b2c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -30128,6 +30128,12 @@ namespace ts { (expr).operand.kind === SyntaxKind.NumericLiteral; } + function isBigIntLiteralExpression(expr: Expression) { + return expr.kind === SyntaxKind.BigIntLiteral || + expr.kind === SyntaxKind.PrefixUnaryExpression && (expr).operator === SyntaxKind.MinusToken && + (expr).operand.kind === SyntaxKind.BigIntLiteral; + } + function isSimpleLiteralEnumReference(expr: Expression) { if ( (isPropertyAccessExpression(expr) || (isElementAccessExpression(expr) && isStringOrNumberLiteralExpression(expr.argumentExpression))) && @@ -30136,19 +30142,25 @@ namespace ts { } function checkAmbientInitializer(node: VariableDeclaration | PropertyDeclaration | PropertySignature) { - if (node.initializer) { - const isInvalidInitializer = !(isStringOrNumberLiteralExpression(node.initializer) || isSimpleLiteralEnumReference(node.initializer) || node.initializer.kind === SyntaxKind.TrueKeyword || node.initializer.kind === SyntaxKind.FalseKeyword); + const {initializer} = node; + if (initializer) { + const isInvalidInitializer = !( + isStringOrNumberLiteralExpression(initializer) || + isSimpleLiteralEnumReference(initializer) || + initializer.kind === SyntaxKind.TrueKeyword || initializer.kind === SyntaxKind.FalseKeyword || + isBigIntLiteralExpression(initializer) + ); const isConstOrReadonly = isDeclarationReadonly(node) || isVariableDeclaration(node) && isVarConst(node); if (isConstOrReadonly && !node.type) { if (isInvalidInitializer) { - return grammarErrorOnNode(node.initializer!, Diagnostics.A_const_initializer_in_an_ambient_context_must_be_a_string_or_numeric_literal_or_literal_enum_reference); + return grammarErrorOnNode(initializer, Diagnostics.A_const_initializer_in_an_ambient_context_must_be_a_string_or_numeric_literal_or_literal_enum_reference); } } else { - return grammarErrorOnNode(node.initializer!, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); + return grammarErrorOnNode(initializer, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); } if (!isConstOrReadonly || isInvalidInitializer) { - return grammarErrorOnNode(node.initializer!, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); + return grammarErrorOnNode(initializer, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); } } } diff --git a/tests/baselines/reference/bigintWithLib.errors.txt b/tests/baselines/reference/bigintWithLib.errors.txt index a24f6ac4802..18267e6ffdf 100644 --- a/tests/baselines/reference/bigintWithLib.errors.txt +++ b/tests/baselines/reference/bigintWithLib.errors.txt @@ -74,4 +74,10 @@ tests/cases/compiler/bigintWithLib.ts(43,26): error TS2345: Argument of type '12 bigintVal = dataView.getBigInt64(1); bigintVal = dataView.getBigInt64(1, true); bigintVal = dataView.getBigUint64(2); - bigintVal = dataView.getBigUint64(2, true); \ No newline at end of file + bigintVal = dataView.getBigUint64(2, true); + + // Test emitted declarations files + const w = 12n; // should emit as const w = 12n + const x = -12n; // should emit as const x = -12n + const y: 12n = 12n; // should emit type 12n + let z = 12n; // should emit type bigint in declaration file \ No newline at end of file diff --git a/tests/baselines/reference/bigintWithLib.js b/tests/baselines/reference/bigintWithLib.js index 979e0d2b129..754b2144f0c 100644 --- a/tests/baselines/reference/bigintWithLib.js +++ b/tests/baselines/reference/bigintWithLib.js @@ -45,7 +45,13 @@ dataView.setBigUint64(2, 123); // should error bigintVal = dataView.getBigInt64(1); bigintVal = dataView.getBigInt64(1, true); bigintVal = dataView.getBigUint64(2); -bigintVal = dataView.getBigUint64(2, true); +bigintVal = dataView.getBigUint64(2, true); + +// Test emitted declarations files +const w = 12n; // should emit as const w = 12n +const x = -12n; // should emit as const x = -12n +const y: 12n = 12n; // should emit type 12n +let z = 12n; // should emit type bigint in declaration file //// [bigintWithLib.js] // Test BigInt functions @@ -92,3 +98,22 @@ bigintVal = dataView.getBigInt64(1); bigintVal = dataView.getBigInt64(1, true); bigintVal = dataView.getBigUint64(2); bigintVal = dataView.getBigUint64(2, true); +// Test emitted declarations files +const w = 12n; // should emit as const w = 12n +const x = -12n; // should emit as const x = -12n +const y = 12n; // should emit type 12n +let z = 12n; // should emit type bigint in declaration file + + +//// [bigintWithLib.d.ts] +declare let bigintVal: bigint; +declare let stringVal: string; +declare let bigIntArray: BigInt64Array; +declare let len: number; +declare let arrayBufferLike: ArrayBufferView; +declare let bigUintArray: BigUint64Array; +declare const dataView: DataView; +declare const w = 12n; +declare const x = -12n; +declare const y: 12n; +declare let z: bigint; diff --git a/tests/baselines/reference/bigintWithLib.symbols b/tests/baselines/reference/bigintWithLib.symbols index 0f313251ab0..cdc80161a45 100644 --- a/tests/baselines/reference/bigintWithLib.symbols +++ b/tests/baselines/reference/bigintWithLib.symbols @@ -204,3 +204,16 @@ bigintVal = dataView.getBigUint64(2, true); >dataView : Symbol(dataView, Decl(bigintWithLib.ts, 36, 5)) >getBigUint64 : Symbol(DataView.getBigUint64, Decl(lib.esnext.bigint.d.ts, --, --)) +// Test emitted declarations files +const w = 12n; // should emit as const w = 12n +>w : Symbol(w, Decl(bigintWithLib.ts, 49, 5)) + +const x = -12n; // should emit as const x = -12n +>x : Symbol(x, Decl(bigintWithLib.ts, 50, 5)) + +const y: 12n = 12n; // should emit type 12n +>y : Symbol(y, Decl(bigintWithLib.ts, 51, 5)) + +let z = 12n; // should emit type bigint in declaration file +>z : Symbol(z, Decl(bigintWithLib.ts, 52, 3)) + diff --git a/tests/baselines/reference/bigintWithLib.types b/tests/baselines/reference/bigintWithLib.types index 5ba273091b6..a1d79119cf6 100644 --- a/tests/baselines/reference/bigintWithLib.types +++ b/tests/baselines/reference/bigintWithLib.types @@ -332,3 +332,21 @@ bigintVal = dataView.getBigUint64(2, true); >2 : 2 >true : true +// Test emitted declarations files +const w = 12n; // should emit as const w = 12n +>w : 12n +>12n : 12n + +const x = -12n; // should emit as const x = -12n +>x : -12n +>-12n : -12n +>12n : 12n + +const y: 12n = 12n; // should emit type 12n +>y : 12n +>12n : 12n + +let z = 12n; // should emit type bigint in declaration file +>z : bigint +>12n : 12n + diff --git a/tests/cases/compiler/bigintWithLib.ts b/tests/cases/compiler/bigintWithLib.ts index 3299b1ecc83..0cefd3094d4 100644 --- a/tests/cases/compiler/bigintWithLib.ts +++ b/tests/cases/compiler/bigintWithLib.ts @@ -1,5 +1,6 @@ // @experimentalBigInt: true // @target: esnext +// @declaration: true // Test BigInt functions let bigintVal: bigint = BigInt(123); @@ -47,4 +48,10 @@ dataView.setBigUint64(2, 123); // should error bigintVal = dataView.getBigInt64(1); bigintVal = dataView.getBigInt64(1, true); bigintVal = dataView.getBigUint64(2); -bigintVal = dataView.getBigUint64(2, true); \ No newline at end of file +bigintVal = dataView.getBigUint64(2, true); + +// Test emitted declarations files +const w = 12n; // should emit as const w = 12n +const x = -12n; // should emit as const x = -12n +const y: 12n = 12n; // should emit type 12n +let z = 12n; // should emit type bigint in declaration file \ No newline at end of file