Skip to content

Treat NoSubstitutionTemplateLiteral same as a StringLiteral #17704

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
Aug 10, 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
16 changes: 7 additions & 9 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17738,15 +17738,14 @@ namespace ts {
return getBestChoiceType(type1, type2);
}

function checkLiteralExpression(node: Expression): Type {
if (node.kind === SyntaxKind.NumericLiteral) {
checkGrammarNumericLiteral(<NumericLiteral>node);
}
function checkLiteralExpression(node: LiteralExpression | Token<SyntaxKind.TrueKeyword | SyntaxKind.FalseKeyword>): Type {
switch (node.kind) {
case SyntaxKind.NoSubstitutionTemplateLiteral:
case SyntaxKind.StringLiteral:
return getFreshTypeOfLiteralType(getLiteralType((<LiteralExpression>node).text));
return getFreshTypeOfLiteralType(getLiteralType(node.text));
case SyntaxKind.NumericLiteral:
return getFreshTypeOfLiteralType(getLiteralType(+(<LiteralExpression>node).text));
checkGrammarNumericLiteral(<NumericLiteral>node);
return getFreshTypeOfLiteralType(getLiteralType(+node.text));
case SyntaxKind.TrueKeyword:
return trueType;
case SyntaxKind.FalseKeyword:
Expand Down Expand Up @@ -17964,15 +17963,14 @@ namespace ts {
return checkSuperExpression(node);
case SyntaxKind.NullKeyword:
return nullWideningType;
case SyntaxKind.NoSubstitutionTemplateLiteral:
case SyntaxKind.StringLiteral:
case SyntaxKind.NumericLiteral:
case SyntaxKind.TrueKeyword:
case SyntaxKind.FalseKeyword:
return checkLiteralExpression(node);
return checkLiteralExpression(node as LiteralExpression);
case SyntaxKind.TemplateExpression:
return checkTemplateExpression(<TemplateExpression>node);
case SyntaxKind.NoSubstitutionTemplateLiteral:
return stringType;
case SyntaxKind.RegularExpressionLiteral:
return globalRegExpType;
case SyntaxKind.ArrayLiteralExpression:
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/asOperator3.types
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var d = `Hello ${123} World` as string;
var e = `Hello` as string;
>e : string
>`Hello` as string : string
>`Hello` : string
>`Hello` : "Hello"

var f = 1 + `${1} end of string` as string;
>f : string
Expand All @@ -59,5 +59,5 @@ var h = tag `Hello` as string;
>tag `Hello` as string : string
>tag `Hello` : any
>tag : (...x: any[]) => any
>`Hello` : string
>`Hello` : "Hello"

2 changes: 1 addition & 1 deletion tests/baselines/reference/asOperatorASI.types
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var x = 10
as `Hello world`; // should not error
>as `Hello world` : any
>as : (...args: any[]) => any
>`Hello world` : string
>`Hello world` : "Hello world"

// Example 2
var y = 20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var v = {
>true : true

[`hello bye`]() { },
>`hello bye` : string
>`hello bye` : "hello bye"

[`hello ${a} bye`]() { }
>`hello ${a} bye` : string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var v = {
>true : true

[`hello bye`]() { },
>`hello bye` : string
>`hello bye` : "hello bye"

[`hello ${a} bye`]() { }
>`hello ${a} bye` : string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var v = {
>0 : 0

set [`hello bye`](v) { },
>`hello bye` : string
>`hello bye` : "hello bye"
>v : any

get [`hello ${a} bye`]() { return 0; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var v = {
>0 : 0

set [`hello bye`](v) { },
>`hello bye` : string
>`hello bye` : "hello bye"
>v : any

get [`hello ${a} bye`]() { return 0; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class C {
>true : true

[`hello bye`]() { }
>`hello bye` : string
>`hello bye` : "hello bye"

static [`hello ${a} bye`]() { }
>`hello ${a} bye` : string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class C {
>true : true

[`hello bye`]() { }
>`hello bye` : string
>`hello bye` : "hello bye"

static [`hello ${a} bye`]() { }
>`hello ${a} bye` : string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class C {
>0 : 0

set [`hello bye`](v) { }
>`hello bye` : string
>`hello bye` : "hello bye"
>v : any

get [`hello ${a} bye`]() { return 0; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class C {
>0 : 0

set [`hello bye`](v) { }
>`hello bye` : string
>`hello bye` : "hello bye"
>v : any

get [`hello ${a} bye`]() { return 0; }
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/computedPropertyNames4_ES5.types
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var v = {
>0 : 0

[`hello bye`]: 0,
>`hello bye` : string
>`hello bye` : "hello bye"
>0 : 0

[`hello ${a} bye`]: 0
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/computedPropertyNames4_ES6.types
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var v = {
>0 : 0

[`hello bye`]: 0,
>`hello bye` : string
>`hello bye` : "hello bye"
>0 : 0

[`hello ${a} bye`]: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
function foo(a = ``) { }
>foo : (a?: string) => void
>a : string
>`` : string
>`` : ""

function bar(a = ``) {
>bar : (a?: string) => void
>a : string
>`` : string
>`` : ""
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings01.ts ===
let ABC: "ABC" = `ABC`;
>ABC : Symbol(ABC, Decl(stringLiteralTypesWithTemplateStrings01.ts, 0, 3))

let DE_NEWLINE_F: "DE\nF" = `DE
>DE_NEWLINE_F : Symbol(DE_NEWLINE_F, Decl(stringLiteralTypesWithTemplateStrings01.ts, 1, 3))

F`;
let G_QUOTE_HI: 'G"HI';
>G_QUOTE_HI : Symbol(G_QUOTE_HI, Decl(stringLiteralTypesWithTemplateStrings01.ts, 3, 3))

let JK_BACKTICK_L: "JK`L" = `JK\`L`;
>JK_BACKTICK_L : Symbol(JK_BACKTICK_L, Decl(stringLiteralTypesWithTemplateStrings01.ts, 4, 3))

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings01.ts ===
let ABC: "ABC" = `ABC`;
>ABC : "ABC"
>`ABC` : "ABC"

let DE_NEWLINE_F: "DE\nF" = `DE
>DE_NEWLINE_F : "DE\nF"
>`DEF` : "DE\nF"

F`;
let G_QUOTE_HI: 'G"HI';
>G_QUOTE_HI : "G\"HI"

let JK_BACKTICK_L: "JK`L" = `JK\`L`;
>JK_BACKTICK_L : "JK`L"
>`JK\`L` : "JK`L"

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings02.ts(1,5): error TS2322: Type 'string' is not assignable to type '"AB\r\nC"'.
tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings02.ts(1,5): error TS2322: Type '"AB\nC"' is not assignable to type '"AB\r\nC"'.
tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings02.ts(3,5): error TS2322: Type 'string' is not assignable to type '"DE\nF"'.


==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings02.ts (2 errors) ====
let abc: "AB\r\nC" = `AB
~~~
!!! error TS2322: Type 'string' is not assignable to type '"AB\r\nC"'.
!!! error TS2322: Type '"AB\nC"' is not assignable to type '"AB\r\nC"'.
C`;
let de_NEWLINE_f: "DE\nF" = `DE${"\n"}F`;
~~~~~~~~~~~~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ function f(...x: any[]) {
f `0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2028 2029 0085 t v f b r n`
>f `0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2028 2029 0085 t v f b r n` : void
>f : (...x: any[]) => void
>`0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2028 2029 0085 t v f b r n` : string
>`0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2028 2029 0085 t v f b r n` : "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2028 2029 0085 t v f b r n"

Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ function f(...x: any[]) {
f `0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2028 2029 0085 t v f b r n`
>f `0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2028 2029 0085 t v f b r n` : void
>f : (...x: any[]) => void
>`0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2028 2029 0085 t v f b r n` : string
>`0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2028 2029 0085 t v f b r n` : "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2028 2029 0085 t v f b r n"

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function f(...args: any[]): void {
f `
>f `\` : void
>f : (...args: any[]) => void
>`\` : string
>`\` : "\n\n"

\

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function f(...args: any[]): void {
f `
>f `\` : void
>f : (...args: any[]) => void
>`\` : string
>`\` : "\n\n"

\

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var f: any;
f `abc`
>f `abc` : any
>f : any
>`abc` : string
>`abc` : "abc"

f `abc${1}def${2}ghi`;
>f `abc${1}def${2}ghi` : any
Expand All @@ -21,7 +21,7 @@ f.g.h `abc`
>f : any
>g : any
>h : any
>`abc` : string
>`abc` : "abc"

f.g.h `abc${1}def${2}ghi`;
>f.g.h `abc${1}def${2}ghi` : any
Expand All @@ -38,7 +38,7 @@ f `abc`.member
>f `abc`.member : any
>f `abc` : any
>f : any
>`abc` : string
>`abc` : "abc"
>member : any

f `abc${1}def${2}ghi`.member;
Expand All @@ -54,7 +54,7 @@ f `abc`["member"];
>f `abc`["member"] : any
>f `abc` : any
>f : any
>`abc` : string
>`abc` : "abc"
>"member" : "member"

f `abc${1}def${2}ghi`["member"];
Expand All @@ -72,7 +72,7 @@ f `abc`["member"].someOtherTag `abc${1}def${2}ghi`;
>f `abc`["member"] : any
>f `abc` : any
>f : any
>`abc` : string
>`abc` : "abc"
>"member" : "member"
>someOtherTag : any
>`abc${1}def${2}ghi` : string
Expand All @@ -99,7 +99,7 @@ f.thisIsNotATag(`abc`);
>f.thisIsNotATag : any
>f : any
>thisIsNotATag : any
>`abc` : string
>`abc` : "abc"

f.thisIsNotATag(`abc${1}def${2}ghi`);
>f.thisIsNotATag(`abc${1}def${2}ghi`) : any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var f: any;
f `abc`
>f `abc` : any
>f : any
>`abc` : string
>`abc` : "abc"

f `abc${1}def${2}ghi`;
>f `abc${1}def${2}ghi` : any
Expand All @@ -21,7 +21,7 @@ f.g.h `abc`
>f : any
>g : any
>h : any
>`abc` : string
>`abc` : "abc"

f.g.h `abc${1}def${2}ghi`;
>f.g.h `abc${1}def${2}ghi` : any
Expand All @@ -38,7 +38,7 @@ f `abc`.member
>f `abc`.member : any
>f `abc` : any
>f : any
>`abc` : string
>`abc` : "abc"
>member : any

f `abc${1}def${2}ghi`.member;
Expand All @@ -54,7 +54,7 @@ f `abc`["member"];
>f `abc`["member"] : any
>f `abc` : any
>f : any
>`abc` : string
>`abc` : "abc"
>"member" : "member"

f `abc${1}def${2}ghi`["member"];
Expand All @@ -72,7 +72,7 @@ f `abc`["member"].someOtherTag `abc${1}def${2}ghi`;
>f `abc`["member"] : any
>f `abc` : any
>f : any
>`abc` : string
>`abc` : "abc"
>"member" : "member"
>someOtherTag : any
>`abc${1}def${2}ghi` : string
Expand All @@ -99,7 +99,7 @@ f.thisIsNotATag(`abc`);
>f.thisIsNotATag : any
>f : any
>thisIsNotATag : any
>`abc` : string
>`abc` : "abc"

f.thisIsNotATag(`abc${1}def${2}ghi`);
>f.thisIsNotATag(`abc${1}def${2}ghi`) : any
Expand Down
Loading