Skip to content

Commit a51c952

Browse files
author
Andy Hanson
committed
Allow template string with no substitutions to be used as a string literal type
1 parent 2a70bf5 commit a51c952

7 files changed

+24
-13
lines changed

src/compiler/factory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,13 +773,13 @@ namespace ts {
773773
: node;
774774
}
775775

776-
export function createLiteralTypeNode(literal: Expression) {
776+
export function createLiteralTypeNode(literal: LiteralTypeNode["literal"]) {
777777
const node = createSynthesizedNode(SyntaxKind.LiteralType) as LiteralTypeNode;
778778
node.literal = literal;
779779
return node;
780780
}
781781

782-
export function updateLiteralTypeNode(node: LiteralTypeNode, literal: Expression) {
782+
export function updateLiteralTypeNode(node: LiteralTypeNode, literal: LiteralTypeNode["literal"]) {
783783
return node.literal !== literal
784784
? updateNode(createLiteralTypeNode(literal), node)
785785
: node;

src/compiler/parser.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2621,16 +2621,9 @@ namespace ts {
26212621
unaryMinusExpression.operator = SyntaxKind.MinusToken;
26222622
nextToken();
26232623
}
2624-
let expression: UnaryExpression;
2625-
switch (token()) {
2626-
case SyntaxKind.StringLiteral:
2627-
case SyntaxKind.NumericLiteral:
2628-
expression = parseLiteralLikeNode(token()) as LiteralExpression;
2629-
break;
2630-
case SyntaxKind.TrueKeyword:
2631-
case SyntaxKind.FalseKeyword:
2632-
expression = parseTokenNode();
2633-
}
2624+
let expression: BooleanLiteral | LiteralExpression | PrefixUnaryExpression = token() === SyntaxKind.TrueKeyword || token() === SyntaxKind.FalseKeyword
2625+
? parseTokenNode<BooleanLiteral>()
2626+
: parseLiteralLikeNode(token()) as LiteralExpression;
26342627
if (negative) {
26352628
unaryMinusExpression.operand = expression;
26362629
finishNode(unaryMinusExpression);
@@ -2666,6 +2659,7 @@ namespace ts {
26662659
return parseJSDocNodeWithType(SyntaxKind.JSDocVariadicType);
26672660
case SyntaxKind.ExclamationToken:
26682661
return parseJSDocNodeWithType(SyntaxKind.JSDocNonNullableType);
2662+
case SyntaxKind.NoSubstitutionTemplateLiteral:
26692663
case SyntaxKind.StringLiteral:
26702664
case SyntaxKind.NumericLiteral:
26712665
case SyntaxKind.TrueKeyword:

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ namespace ts {
10491049

10501050
export interface LiteralTypeNode extends TypeNode {
10511051
kind: SyntaxKind.LiteralType;
1052-
literal: Expression;
1052+
literal: BooleanLiteral | LiteralExpression | PrefixUnaryExpression;
10531053
}
10541054

10551055
export interface StringLiteral extends LiteralExpression {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [noSubstitutionTemplateStringLiteralTypes.ts]
2+
const x: `foo` = "foo";
3+
4+
5+
//// [noSubstitutionTemplateStringLiteralTypes.js]
6+
var x = "foo";
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/compiler/noSubstitutionTemplateStringLiteralTypes.ts ===
2+
const x: `foo` = "foo";
3+
>x : Symbol(x, Decl(noSubstitutionTemplateStringLiteralTypes.ts, 0, 5))
4+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/compiler/noSubstitutionTemplateStringLiteralTypes.ts ===
2+
const x: `foo` = "foo";
3+
>x : "foo"
4+
>`foo` : "foo"
5+
>"foo" : "foo"
6+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const x: `foo` = "foo";

0 commit comments

Comments
 (0)