@@ -2015,6 +2015,10 @@ namespace ts {
2015
2015
isExternalModuleAugmentation(node.parent.parent);
2016
2016
}
2017
2017
2018
+ function literalTypeToString(type: LiteralType) {
2019
+ return type.flags & TypeFlags.StringLiteral ? `"${escapeString((<LiteralType>type).text)}"` : (<LiteralType>type).text;
2020
+ }
2021
+
2018
2022
function getSymbolDisplayBuilder(): SymbolDisplayBuilder {
2019
2023
2020
2024
function getNameOfSymbol(symbol: Symbol): string {
@@ -2190,11 +2194,8 @@ namespace ts {
2190
2194
else if (type.flags & TypeFlags.Anonymous) {
2191
2195
writeAnonymousType(<ObjectType>type, nextFlags);
2192
2196
}
2193
- else if (type.flags & TypeFlags.StringLiteral) {
2194
- writer.writeStringLiteral(`"${escapeString((<LiteralType>type).text)}"`);
2195
- }
2196
- else if (type.flags & TypeFlags.NumberLiteral) {
2197
- writer.writeStringLiteral((<LiteralType>type).text);
2197
+ else if (type.flags & TypeFlags.StringOrNumberLiteral) {
2198
+ writer.writeStringLiteral(literalTypeToString(<LiteralType>type));
2198
2199
}
2199
2200
else {
2200
2201
// Should never get here
@@ -19015,6 +19016,19 @@ namespace ts {
19015
19016
return undefined;
19016
19017
}
19017
19018
19019
+ function isLiteralConstDeclaration(node: VariableDeclaration): boolean {
19020
+ if (isConst(node)) {
19021
+ const type = getTypeOfSymbol(getSymbolOfNode(node));
19022
+ return !!(type.flags & TypeFlags.StringOrNumberLiteral && type.flags & TypeFlags.FreshLiteral);
19023
+ }
19024
+ return false;
19025
+ }
19026
+
19027
+ function writeLiteralConstValue(node: VariableDeclaration, writer: SymbolWriter) {
19028
+ const type = getTypeOfSymbol(getSymbolOfNode(node));
19029
+ writer.writeStringLiteral(literalTypeToString(<LiteralType>type));
19030
+ }
19031
+
19018
19032
function createResolver(): EmitResolver {
19019
19033
// this variable and functions that use it are deliberately moved here from the outer scope
19020
19034
// to avoid scope pollution
@@ -19059,7 +19073,9 @@ namespace ts {
19059
19073
isArgumentsLocalBinding,
19060
19074
getExternalModuleFileFromDeclaration,
19061
19075
getTypeReferenceDirectivesForEntityName,
19062
- getTypeReferenceDirectivesForSymbol
19076
+ getTypeReferenceDirectivesForSymbol,
19077
+ isLiteralConstDeclaration,
19078
+ writeLiteralConstValue
19063
19079
};
19064
19080
19065
19081
// defined here to avoid outer scope pollution
@@ -20205,10 +20221,29 @@ namespace ts {
20205
20221
}
20206
20222
}
20207
20223
20224
+ function isStringOrNumberLiteralExpression(expr: Expression) {
20225
+ return expr.kind === SyntaxKind.StringLiteral || expr.kind === SyntaxKind.NumericLiteral ||
20226
+ expr.kind === SyntaxKind.PrefixUnaryExpression && (<PrefixUnaryExpression>expr).operator === SyntaxKind.MinusToken &&
20227
+ (<PrefixUnaryExpression>expr).operand.kind === SyntaxKind.NumericLiteral;
20228
+ }
20229
+
20208
20230
function checkGrammarVariableDeclaration(node: VariableDeclaration) {
20209
20231
if (node.parent.parent.kind !== SyntaxKind.ForInStatement && node.parent.parent.kind !== SyntaxKind.ForOfStatement) {
20210
20232
if (isInAmbientContext(node)) {
20211
20233
if (node.initializer) {
20234
+ if (isConst(node) && !node.type) {
20235
+ if (!isStringOrNumberLiteralExpression(node.initializer)) {
20236
+ return grammarErrorOnNode(node.initializer, Diagnostics.A_const_initializer_in_an_ambient_context_must_be_a_string_or_numeric_literal);
20237
+ }
20238
+ }
20239
+ else {
20240
+ // Error on equals token which immediate precedes the initializer
20241
+ const equalsTokenLength = "=".length;
20242
+ return grammarErrorAtPos(getSourceFileOfNode(node), node.initializer.pos - equalsTokenLength,
20243
+ equalsTokenLength, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
20244
+ }
20245
+ }
20246
+ if (node.initializer && !(isConst(node) && isStringOrNumberLiteralExpression(node.initializer))) {
20212
20247
// Error on equals token which immediate precedes the initializer
20213
20248
const equalsTokenLength = "=".length;
20214
20249
return grammarErrorAtPos(getSourceFileOfNode(node), node.initializer.pos - equalsTokenLength,
0 commit comments