diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d1a442e8eccc6..ea11a4c10cf78 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -21706,7 +21706,7 @@ namespace ts { const initializer = getEffectiveInitializer(declaration)!; const type = getTypeOfExpression(initializer, /*cache*/ true); const widened = getCombinedNodeFlags(declaration) & NodeFlags.Const || - (getCombinedModifierFlags(declaration) & ModifierFlags.Readonly && !isParameterPropertyDeclaration(declaration)) || + isDeclarationReadonly(declaration) || isTypeAssertion(initializer) ? type : getWidenedLiteralType(type); if (isInJavaScriptFile(declaration)) { if (widened.flags & TypeFlags.Nullable) { @@ -28080,7 +28080,7 @@ namespace ts { } function isLiteralConstDeclaration(node: VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration): boolean { - if (isVariableDeclaration(node) && isVarConst(node)) { + if (isDeclarationReadonly(node) || isVariableDeclaration(node) && isVarConst(node)) { const type = getTypeOfSymbol(getSymbolOfNode(node)); return !!(type.flags & TypeFlags.StringOrNumberLiteral && type.flags & TypeFlags.FreshLiteral); } @@ -29418,26 +29418,28 @@ namespace ts { (expr).operand.kind === SyntaxKind.NumericLiteral; } + function checkAmbientInitializer(node: VariableDeclaration | PropertyDeclaration | PropertySignature) { + if (node.initializer) { + const isInvalidInitializer = !isStringOrNumberLiteralExpression(node.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); + } + } + else { + return grammarErrorOnNode(node.initializer!, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); + } + if (!isConstOrReadonly || isInvalidInitializer) { + return grammarErrorOnNode(node.initializer!, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); + } + } + } + function checkGrammarVariableDeclaration(node: VariableDeclaration) { if (node.parent.parent.kind !== SyntaxKind.ForInStatement && node.parent.parent.kind !== SyntaxKind.ForOfStatement) { if (node.flags & NodeFlags.Ambient) { - if (node.initializer) { - if (isVarConst(node) && !node.type) { - if (!isStringOrNumberLiteralExpression(node.initializer)) { - return grammarErrorOnNode(node.initializer, Diagnostics.A_const_initializer_in_an_ambient_context_must_be_a_string_or_numeric_literal); - } - } - else { - // Error on equals token which immediate precedes the initializer - const equalsTokenLength = "=".length; - return grammarErrorAtPos(node, node.initializer.pos - equalsTokenLength, equalsTokenLength, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); - } - } - if (node.initializer && !(isVarConst(node) && isStringOrNumberLiteralExpression(node.initializer))) { - // Error on equals token which immediate precedes the initializer - const equalsTokenLength = "=".length; - return grammarErrorAtPos(node, node.initializer.pos - equalsTokenLength, equalsTokenLength, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); - } + checkAmbientInitializer(node); } else if (!node.initializer) { if (isBindingPattern(node.name) && !isBindingPattern(node.parent)) { @@ -29630,8 +29632,8 @@ namespace ts { } } - if (node.flags & NodeFlags.Ambient && node.initializer) { - return grammarErrorOnFirstToken(node.initializer, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); + if (node.flags & NodeFlags.Ambient) { + checkAmbientInitializer(node); } if (isPropertyDeclaration(node) && node.exclamationToken && (!isClassLike(node.parent) || !node.type || node.initializer || diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 3d5893d0a03fc..fb0ccd231608e 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -922,6 +922,10 @@ namespace ts { return !!(getCombinedModifierFlags(node) & ModifierFlags.Const); } + export function isDeclarationReadonly(declaration: Declaration): boolean { + return !!(getCombinedModifierFlags(declaration) & ModifierFlags.Readonly && !isParameterPropertyDeclaration(declaration)); + } + export function isVarConst(node: VariableDeclaration | VariableDeclarationList): boolean { return !!(getCombinedNodeFlags(node) & NodeFlags.Const); } diff --git a/tests/baselines/reference/ambientErrors.errors.txt b/tests/baselines/reference/ambientErrors.errors.txt index 7b42085e0c20b..05e72ccc879ad 100644 --- a/tests/baselines/reference/ambientErrors.errors.txt +++ b/tests/baselines/reference/ambientErrors.errors.txt @@ -1,8 +1,8 @@ -tests/cases/conformance/ambient/ambientErrors.ts(2,15): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/conformance/ambient/ambientErrors.ts(2,17): error TS1039: Initializers are not allowed in ambient contexts. tests/cases/conformance/ambient/ambientErrors.ts(17,22): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. tests/cases/conformance/ambient/ambientErrors.ts(20,24): error TS1183: An implementation cannot be declared in ambient contexts. tests/cases/conformance/ambient/ambientErrors.ts(29,9): error TS1066: In ambient enum declarations member initializer must be constant expression. -tests/cases/conformance/ambient/ambientErrors.ts(34,11): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/conformance/ambient/ambientErrors.ts(34,13): error TS1039: Initializers are not allowed in ambient contexts. tests/cases/conformance/ambient/ambientErrors.ts(35,19): error TS1183: An implementation cannot be declared in ambient contexts. tests/cases/conformance/ambient/ambientErrors.ts(37,20): error TS1039: Initializers are not allowed in ambient contexts. tests/cases/conformance/ambient/ambientErrors.ts(38,13): error TS1039: Initializers are not allowed in ambient contexts. @@ -17,7 +17,7 @@ tests/cases/conformance/ambient/ambientErrors.ts(57,5): error TS2309: An export ==== tests/cases/conformance/ambient/ambientErrors.ts (14 errors) ==== // Ambient variable with an initializer declare var x = 4; - ~ + ~ !!! error TS1039: Initializers are not allowed in ambient contexts. // Ambient functions with invalid overloads @@ -57,7 +57,7 @@ tests/cases/conformance/ambient/ambientErrors.ts(57,5): error TS2309: An export // Ambient module with initializers for values, bodies for functions / classes declare module M1 { var x = 3; - ~ + ~ !!! error TS1039: Initializers are not allowed in ambient contexts. function fn() { } ~ diff --git a/tests/baselines/reference/ambientErrors1.errors.txt b/tests/baselines/reference/ambientErrors1.errors.txt index db7a155cc3fec..5ae2b615eecf8 100644 --- a/tests/baselines/reference/ambientErrors1.errors.txt +++ b/tests/baselines/reference/ambientErrors1.errors.txt @@ -1,7 +1,7 @@ -tests/cases/compiler/ambientErrors1.ts(1,15): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/ambientErrors1.ts(1,17): error TS1039: Initializers are not allowed in ambient contexts. ==== tests/cases/compiler/ambientErrors1.ts (1 errors) ==== declare var x = 4; - ~ + ~ !!! error TS1039: Initializers are not allowed in ambient contexts. \ No newline at end of file diff --git a/tests/baselines/reference/ambientStatement1.errors.txt b/tests/baselines/reference/ambientStatement1.errors.txt index be7c7bb750c50..1a5fb6441136c 100644 --- a/tests/baselines/reference/ambientStatement1.errors.txt +++ b/tests/baselines/reference/ambientStatement1.errors.txt @@ -1,5 +1,5 @@ tests/cases/compiler/ambientStatement1.ts(2,6): error TS1036: Statements are not allowed in ambient contexts. -tests/cases/compiler/ambientStatement1.ts(4,20): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/ambientStatement1.ts(4,22): error TS1039: Initializers are not allowed in ambient contexts. ==== tests/cases/compiler/ambientStatement1.ts (2 errors) ==== @@ -9,6 +9,6 @@ tests/cases/compiler/ambientStatement1.ts(4,20): error TS1039: Initializers are !!! error TS1036: Statements are not allowed in ambient contexts. export var v1 = () => false; - ~ + ~~~~~~~~~~~ !!! error TS1039: Initializers are not allowed in ambient contexts. } \ No newline at end of file diff --git a/tests/baselines/reference/constDeclarations-ambient-errors.errors.txt b/tests/baselines/reference/constDeclarations-ambient-errors.errors.txt index 979448abf7296..8d2b9d67ed8ae 100644 --- a/tests/baselines/reference/constDeclarations-ambient-errors.errors.txt +++ b/tests/baselines/reference/constDeclarations-ambient-errors.errors.txt @@ -1,30 +1,30 @@ -tests/cases/compiler/constDeclarations-ambient-errors.ts(2,27): error TS1039: Initializers are not allowed in ambient contexts. -tests/cases/compiler/constDeclarations-ambient-errors.ts(3,26): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/constDeclarations-ambient-errors.ts(2,29): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/constDeclarations-ambient-errors.ts(3,28): error TS1039: Initializers are not allowed in ambient contexts. tests/cases/compiler/constDeclarations-ambient-errors.ts(4,20): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal. -tests/cases/compiler/constDeclarations-ambient-errors.ts(4,37): error TS1039: Initializers are not allowed in ambient contexts. -tests/cases/compiler/constDeclarations-ambient-errors.ts(4,51): error TS1039: Initializers are not allowed in ambient contexts. -tests/cases/compiler/constDeclarations-ambient-errors.ts(8,22): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/constDeclarations-ambient-errors.ts(4,39): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/constDeclarations-ambient-errors.ts(4,53): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/constDeclarations-ambient-errors.ts(8,24): error TS1039: Initializers are not allowed in ambient contexts. ==== tests/cases/compiler/constDeclarations-ambient-errors.ts (6 errors) ==== // error: no intialization expected in ambient declarations declare const c1: boolean = true; - ~ + ~~~~ !!! error TS1039: Initializers are not allowed in ambient contexts. declare const c2: number = 0; - ~ + ~ !!! error TS1039: Initializers are not allowed in ambient contexts. declare const c3 = null, c4 :string = "", c5: any = 0; ~~~~ !!! error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal. - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. - ~ + ~ !!! error TS1039: Initializers are not allowed in ambient contexts. declare module M { const c6 = 0; const c7: number = 7; - ~ + ~ !!! error TS1039: Initializers are not allowed in ambient contexts. } \ No newline at end of file diff --git a/tests/baselines/reference/declarationEmitConstantNoWidening.js b/tests/baselines/reference/declarationEmitConstantNoWidening.js new file mode 100644 index 0000000000000..268f07765437a --- /dev/null +++ b/tests/baselines/reference/declarationEmitConstantNoWidening.js @@ -0,0 +1,24 @@ +//// [declarationEmitConstantNoWidening.ts] +export const FOO = 'FOO'; +export class Bar { + readonly type = FOO; // Should be widening literal "FOO" - so either `typeof "FOO"` or = "FOO" +} + +//// [declarationEmitConstantNoWidening.js] +"use strict"; +exports.__esModule = true; +exports.FOO = 'FOO'; +var Bar = /** @class */ (function () { + function Bar() { + this.type = exports.FOO; // Should be widening literal "FOO" - so either `typeof "FOO"` or = "FOO" + } + return Bar; +}()); +exports.Bar = Bar; + + +//// [declarationEmitConstantNoWidening.d.ts] +export declare const FOO = "FOO"; +export declare class Bar { + readonly type = "FOO"; +} diff --git a/tests/baselines/reference/declarationEmitConstantNoWidening.symbols b/tests/baselines/reference/declarationEmitConstantNoWidening.symbols new file mode 100644 index 0000000000000..17ab38272b9f6 --- /dev/null +++ b/tests/baselines/reference/declarationEmitConstantNoWidening.symbols @@ -0,0 +1,11 @@ +=== tests/cases/compiler/declarationEmitConstantNoWidening.ts === +export const FOO = 'FOO'; +>FOO : Symbol(FOO, Decl(declarationEmitConstantNoWidening.ts, 0, 12)) + +export class Bar { +>Bar : Symbol(Bar, Decl(declarationEmitConstantNoWidening.ts, 0, 25)) + + readonly type = FOO; // Should be widening literal "FOO" - so either `typeof "FOO"` or = "FOO" +>type : Symbol(Bar.type, Decl(declarationEmitConstantNoWidening.ts, 1, 18)) +>FOO : Symbol(FOO, Decl(declarationEmitConstantNoWidening.ts, 0, 12)) +} diff --git a/tests/baselines/reference/declarationEmitConstantNoWidening.types b/tests/baselines/reference/declarationEmitConstantNoWidening.types new file mode 100644 index 0000000000000..5a7a11728a51f --- /dev/null +++ b/tests/baselines/reference/declarationEmitConstantNoWidening.types @@ -0,0 +1,12 @@ +=== tests/cases/compiler/declarationEmitConstantNoWidening.ts === +export const FOO = 'FOO'; +>FOO : "FOO" +>'FOO' : "FOO" + +export class Bar { +>Bar : Bar + + readonly type = FOO; // Should be widening literal "FOO" - so either `typeof "FOO"` or = "FOO" +>type : "FOO" +>FOO : "FOO" +} diff --git a/tests/baselines/reference/externSemantics.errors.txt b/tests/baselines/reference/externSemantics.errors.txt index b58e2a4950c93..db6ce0f6acda8 100644 --- a/tests/baselines/reference/externSemantics.errors.txt +++ b/tests/baselines/reference/externSemantics.errors.txt @@ -1,13 +1,13 @@ -tests/cases/compiler/externSemantics.ts(1,14): error TS1039: Initializers are not allowed in ambient contexts. -tests/cases/compiler/externSemantics.ts(3,21): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/externSemantics.ts(1,15): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/externSemantics.ts(3,22): error TS1039: Initializers are not allowed in ambient contexts. ==== tests/cases/compiler/externSemantics.ts (2 errors) ==== declare var x=10; - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. declare var v; declare var y:number=3; - ~ + ~ !!! error TS1039: Initializers are not allowed in ambient contexts. \ No newline at end of file diff --git a/tests/baselines/reference/initializersInDeclarations.errors.txt b/tests/baselines/reference/initializersInDeclarations.errors.txt index 34a7b67e5d59c..d7f2847286b69 100644 --- a/tests/baselines/reference/initializersInDeclarations.errors.txt +++ b/tests/baselines/reference/initializersInDeclarations.errors.txt @@ -1,10 +1,10 @@ tests/cases/conformance/externalModules/file1.d.ts(4,9): error TS1039: Initializers are not allowed in ambient contexts. tests/cases/conformance/externalModules/file1.d.ts(5,16): error TS1039: Initializers are not allowed in ambient contexts. tests/cases/conformance/externalModules/file1.d.ts(6,16): error TS1183: An implementation cannot be declared in ambient contexts. -tests/cases/conformance/externalModules/file1.d.ts(11,15): error TS1039: Initializers are not allowed in ambient contexts. -tests/cases/conformance/externalModules/file1.d.ts(12,15): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/conformance/externalModules/file1.d.ts(11,17): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/conformance/externalModules/file1.d.ts(12,17): error TS1039: Initializers are not allowed in ambient contexts. tests/cases/conformance/externalModules/file1.d.ts(15,2): error TS1036: Statements are not allowed in ambient contexts. -tests/cases/conformance/externalModules/file1.d.ts(17,16): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/conformance/externalModules/file1.d.ts(17,18): error TS1039: Initializers are not allowed in ambient contexts. ==== tests/cases/conformance/externalModules/file1.d.ts (7 errors) ==== @@ -25,10 +25,10 @@ tests/cases/conformance/externalModules/file1.d.ts(17,16): error TS1039: Initial } declare var x = []; - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. declare var y = {}; - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. declare module M1 { @@ -37,6 +37,6 @@ tests/cases/conformance/externalModules/file1.d.ts(17,16): error TS1039: Initial !!! error TS1036: Statements are not allowed in ambient contexts. export var v1 = () => false; - ~ + ~~~~~~~~~~~ !!! error TS1039: Initializers are not allowed in ambient contexts. } \ No newline at end of file diff --git a/tests/baselines/reference/missingRequiredDeclare.d.errors.txt b/tests/baselines/reference/missingRequiredDeclare.d.errors.txt index dc713c86d8904..a673c948a4873 100644 --- a/tests/baselines/reference/missingRequiredDeclare.d.errors.txt +++ b/tests/baselines/reference/missingRequiredDeclare.d.errors.txt @@ -1,10 +1,10 @@ tests/cases/compiler/missingRequiredDeclare.d.ts(1,1): error TS1046: A 'declare' modifier is required for a top level declaration in a .d.ts file. -tests/cases/compiler/missingRequiredDeclare.d.ts(1,7): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/missingRequiredDeclare.d.ts(1,9): error TS1039: Initializers are not allowed in ambient contexts. ==== tests/cases/compiler/missingRequiredDeclare.d.ts (2 errors) ==== var x = 1; ~~~ !!! error TS1046: A 'declare' modifier is required for a top level declaration in a .d.ts file. - ~ + ~ !!! error TS1039: Initializers are not allowed in ambient contexts. \ No newline at end of file diff --git a/tests/baselines/reference/parserModifierOnStatementInBlock2.errors.txt b/tests/baselines/reference/parserModifierOnStatementInBlock2.errors.txt index d863bf7143e86..c990b08efc15b 100644 --- a/tests/baselines/reference/parserModifierOnStatementInBlock2.errors.txt +++ b/tests/baselines/reference/parserModifierOnStatementInBlock2.errors.txt @@ -1,5 +1,5 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserModifierOnStatementInBlock2.ts(2,4): error TS1184: Modifiers cannot appear here. -tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserModifierOnStatementInBlock2.ts(2,18): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserModifierOnStatementInBlock2.ts(2,20): error TS1039: Initializers are not allowed in ambient contexts. ==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserModifierOnStatementInBlock2.ts (2 errors) ==== @@ -7,7 +7,7 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserModifierOnStateme declare var x = this; ~~~~~~~ !!! error TS1184: Modifiers cannot appear here. - ~ + ~~~~ !!! error TS1039: Initializers are not allowed in ambient contexts. } \ No newline at end of file diff --git a/tests/baselines/reference/parserVariableStatement1.d.errors.txt b/tests/baselines/reference/parserVariableStatement1.d.errors.txt index de0bef5288c53..eb6f799df74ab 100644 --- a/tests/baselines/reference/parserVariableStatement1.d.errors.txt +++ b/tests/baselines/reference/parserVariableStatement1.d.errors.txt @@ -1,10 +1,10 @@ tests/cases/conformance/parser/ecmascript5/Statements/parserVariableStatement1.d.ts(1,1): error TS1046: A 'declare' modifier is required for a top level declaration in a .d.ts file. -tests/cases/conformance/parser/ecmascript5/Statements/parserVariableStatement1.d.ts(1,7): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/conformance/parser/ecmascript5/Statements/parserVariableStatement1.d.ts(1,9): error TS1039: Initializers are not allowed in ambient contexts. ==== tests/cases/conformance/parser/ecmascript5/Statements/parserVariableStatement1.d.ts (2 errors) ==== var v = 1; ~~~ !!! error TS1046: A 'declare' modifier is required for a top level declaration in a .d.ts file. - ~ + ~ !!! error TS1039: Initializers are not allowed in ambient contexts. \ No newline at end of file diff --git a/tests/baselines/reference/varBlock.errors.txt b/tests/baselines/reference/varBlock.errors.txt index 6746a760a44ee..12e3fc6a629f6 100644 --- a/tests/baselines/reference/varBlock.errors.txt +++ b/tests/baselines/reference/varBlock.errors.txt @@ -1,23 +1,23 @@ -tests/cases/compiler/varBlock.ts(8,16): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/varBlock.ts(8,18): error TS1039: Initializers are not allowed in ambient contexts. tests/cases/compiler/varBlock.ts(11,22): error TS2369: A parameter property is only allowed in a constructor implementation. tests/cases/compiler/varBlock.ts(11,22): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. -tests/cases/compiler/varBlock.ts(15,15): error TS1039: Initializers are not allowed in ambient contexts. -tests/cases/compiler/varBlock.ts(21,16): error TS1039: Initializers are not allowed in ambient contexts. -tests/cases/compiler/varBlock.ts(22,20): error TS1039: Initializers are not allowed in ambient contexts. -tests/cases/compiler/varBlock.ts(25,19): error TS1039: Initializers are not allowed in ambient contexts. -tests/cases/compiler/varBlock.ts(26,24): error TS1039: Initializers are not allowed in ambient contexts. -tests/cases/compiler/varBlock.ts(26,33): error TS1039: Initializers are not allowed in ambient contexts. -tests/cases/compiler/varBlock.ts(27,27): error TS1039: Initializers are not allowed in ambient contexts. -tests/cases/compiler/varBlock.ts(28,33): error TS1039: Initializers are not allowed in ambient contexts. -tests/cases/compiler/varBlock.ts(28,43): error TS1039: Initializers are not allowed in ambient contexts. -tests/cases/compiler/varBlock.ts(32,11): error TS1039: Initializers are not allowed in ambient contexts. -tests/cases/compiler/varBlock.ts(33,16): error TS1039: Initializers are not allowed in ambient contexts. -tests/cases/compiler/varBlock.ts(33,25): error TS1039: Initializers are not allowed in ambient contexts. -tests/cases/compiler/varBlock.ts(34,19): error TS1039: Initializers are not allowed in ambient contexts. -tests/cases/compiler/varBlock.ts(35,25): error TS1039: Initializers are not allowed in ambient contexts. -tests/cases/compiler/varBlock.ts(35,35): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/varBlock.ts(15,17): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/varBlock.ts(21,18): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/varBlock.ts(22,22): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/varBlock.ts(25,21): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/varBlock.ts(26,26): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/varBlock.ts(26,35): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/varBlock.ts(27,29): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/varBlock.ts(28,35): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/varBlock.ts(28,45): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/varBlock.ts(32,13): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/varBlock.ts(33,18): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/varBlock.ts(33,26): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/varBlock.ts(34,21): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/varBlock.ts(35,27): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/varBlock.ts(35,37): error TS1039: Initializers are not allowed in ambient contexts. tests/cases/compiler/varBlock.ts(39,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'c' must be of type 'any', but here has type 'number'. -tests/cases/compiler/varBlock.ts(39,15): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/compiler/varBlock.ts(39,17): error TS1039: Initializers are not allowed in ambient contexts. ==== tests/cases/compiler/varBlock.ts (20 errors) ==== @@ -29,7 +29,7 @@ tests/cases/compiler/varBlock.ts(39,15): error TS1039: Initializers are not allo declare module m3 { var a, b, c; var a1, b1 = 10; - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. class C { @@ -42,7 +42,7 @@ tests/cases/compiler/varBlock.ts(39,15): error TS1039: Initializers are not allo } declare var b = 10; - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. declare var a2, b2, c2; @@ -50,47 +50,47 @@ tests/cases/compiler/varBlock.ts(39,15): error TS1039: Initializers are not allo declare var da = 10; - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. declare var d3, d4 = 10; - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. module m3 { declare var d = 10; - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. declare var d2, d3 = 10, d4 = 10; - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. export declare var dE = 10; - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. export declare var d2E, d3E = 10, d4E = 10; - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. } declare module m4 { var d = 10; - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. var d2, d3 = 10, d4 =10; - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. export var dE = 10; - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. export var d2E, d3E = 10, d4E = 10; - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. } @@ -98,5 +98,5 @@ tests/cases/compiler/varBlock.ts(39,15): error TS1039: Initializers are not allo declare var c = 10; ~ !!! error TS2403: Subsequent variable declarations must have the same type. Variable 'c' must be of type 'any', but here has type 'number'. - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. \ No newline at end of file diff --git a/tests/cases/compiler/declarationEmitConstantNoWidening.ts b/tests/cases/compiler/declarationEmitConstantNoWidening.ts new file mode 100644 index 0000000000000..6bf6e92c7d409 --- /dev/null +++ b/tests/cases/compiler/declarationEmitConstantNoWidening.ts @@ -0,0 +1,5 @@ +// @declaration: true +export const FOO = 'FOO'; +export class Bar { + readonly type = FOO; // Should be widening literal "FOO" - so either `typeof "FOO"` or = "FOO" +} \ No newline at end of file