Allow template string with no substitutions to be used as a string literal type (#18452)

This commit is contained in:
Andy 2017-09-14 11:19:54 -07:00 committed by GitHub
parent 6e512a495f
commit e91af7d30d
7 changed files with 24 additions and 13 deletions

View File

@ -773,13 +773,13 @@ namespace ts {
: node;
}
export function createLiteralTypeNode(literal: Expression) {
export function createLiteralTypeNode(literal: LiteralTypeNode["literal"]) {
const node = createSynthesizedNode(SyntaxKind.LiteralType) as LiteralTypeNode;
node.literal = literal;
return node;
}
export function updateLiteralTypeNode(node: LiteralTypeNode, literal: Expression) {
export function updateLiteralTypeNode(node: LiteralTypeNode, literal: LiteralTypeNode["literal"]) {
return node.literal !== literal
? updateNode(createLiteralTypeNode(literal), node)
: node;

View File

@ -2621,16 +2621,9 @@ namespace ts {
unaryMinusExpression.operator = SyntaxKind.MinusToken;
nextToken();
}
let expression: UnaryExpression;
switch (token()) {
case SyntaxKind.StringLiteral:
case SyntaxKind.NumericLiteral:
expression = parseLiteralLikeNode(token()) as LiteralExpression;
break;
case SyntaxKind.TrueKeyword:
case SyntaxKind.FalseKeyword:
expression = parseTokenNode();
}
let expression: BooleanLiteral | LiteralExpression | PrefixUnaryExpression = token() === SyntaxKind.TrueKeyword || token() === SyntaxKind.FalseKeyword
? parseTokenNode<BooleanLiteral>()
: parseLiteralLikeNode(token()) as LiteralExpression;
if (negative) {
unaryMinusExpression.operand = expression;
finishNode(unaryMinusExpression);
@ -2666,6 +2659,7 @@ namespace ts {
return parseJSDocNodeWithType(SyntaxKind.JSDocVariadicType);
case SyntaxKind.ExclamationToken:
return parseJSDocNodeWithType(SyntaxKind.JSDocNonNullableType);
case SyntaxKind.NoSubstitutionTemplateLiteral:
case SyntaxKind.StringLiteral:
case SyntaxKind.NumericLiteral:
case SyntaxKind.TrueKeyword:

View File

@ -1049,7 +1049,7 @@ namespace ts {
export interface LiteralTypeNode extends TypeNode {
kind: SyntaxKind.LiteralType;
literal: Expression;
literal: BooleanLiteral | LiteralExpression | PrefixUnaryExpression;
}
export interface StringLiteral extends LiteralExpression {

View File

@ -0,0 +1,6 @@
//// [noSubstitutionTemplateStringLiteralTypes.ts]
const x: `foo` = "foo";
//// [noSubstitutionTemplateStringLiteralTypes.js]
var x = "foo";

View File

@ -0,0 +1,4 @@
=== tests/cases/compiler/noSubstitutionTemplateStringLiteralTypes.ts ===
const x: `foo` = "foo";
>x : Symbol(x, Decl(noSubstitutionTemplateStringLiteralTypes.ts, 0, 5))

View File

@ -0,0 +1,6 @@
=== tests/cases/compiler/noSubstitutionTemplateStringLiteralTypes.ts ===
const x: `foo` = "foo";
>x : "foo"
>`foo` : "foo"
>"foo" : "foo"

View File

@ -0,0 +1 @@
const x: `foo` = "foo";