Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from
Sep 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiler/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 4 additions & 10 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//// [noSubstitutionTemplateStringLiteralTypes.ts]
const x: `foo` = "foo";


//// [noSubstitutionTemplateStringLiteralTypes.js]
var x = "foo";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
=== tests/cases/compiler/noSubstitutionTemplateStringLiteralTypes.ts ===
const x: `foo` = "foo";
>x : Symbol(x, Decl(noSubstitutionTemplateStringLiteralTypes.ts, 0, 5))

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
=== tests/cases/compiler/noSubstitutionTemplateStringLiteralTypes.ts ===
const x: `foo` = "foo";
>x : "foo"
>`foo` : "foo"
>"foo" : "foo"

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const x: `foo` = "foo";