Skip to content

Commit

Permalink
Check for symbol types in template expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-williams committed Mar 1, 2018
1 parent c12fc0d commit fc08ff5
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19361,7 +19361,9 @@ namespace ts {
// A place where we actually *are* concerned with the expressions' types are
// in tagged templates.
forEach(node.templateSpans, templateSpan => {
checkExpression(templateSpan.expression);
if (maybeTypeOfKind(checkExpression(templateSpan.expression), TypeFlags.ESSymbolLike)) {
error(templateSpan.expression, Diagnostics.Type_0_cannot_be_converted_to_type_1, typeToString(esSymbolType), typeToString(stringType));
}
});

return stringType;
Expand Down
21 changes: 21 additions & 0 deletions tests/baselines/reference/noImplicitSymbolToString.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
tests/cases/compiler/noImplicitSymbolToString.ts(6,30): error TS2352: Type 'symbol' cannot be converted to type 'string'.
tests/cases/compiler/noImplicitSymbolToString.ts(7,30): error TS2469: The '+' operator cannot be applied to type 'symbol'.
tests/cases/compiler/noImplicitSymbolToString.ts(8,8): error TS2469: The '+=' operator cannot be applied to type 'symbol'.


==== tests/cases/compiler/noImplicitSymbolToString.ts (3 errors) ====
// Fix #19666

let symbol!: symbol;
let str = "hello ";

const templateStr = `hello ${symbol}`;
~~~~~~
!!! error TS2352: Type 'symbol' cannot be converted to type 'string'.
const appendStr = "hello " + symbol;
~~~~~~
!!! error TS2469: The '+' operator cannot be applied to type 'symbol'.
str += symbol;
~~~~~~
!!! error TS2469: The '+=' operator cannot be applied to type 'symbol'.

18 changes: 18 additions & 0 deletions tests/baselines/reference/noImplicitSymbolToString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//// [noImplicitSymbolToString.ts]
// Fix #19666

let symbol!: symbol;
let str = "hello ";

const templateStr = `hello ${symbol}`;
const appendStr = "hello " + symbol;
str += symbol;


//// [noImplicitSymbolToString.js]
// Fix #19666
var symbol;
var str = "hello ";
var templateStr = "hello " + symbol;
var appendStr = "hello " + symbol;
str += symbol;
21 changes: 21 additions & 0 deletions tests/baselines/reference/noImplicitSymbolToString.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== tests/cases/compiler/noImplicitSymbolToString.ts ===
// Fix #19666

let symbol!: symbol;
>symbol : Symbol(symbol, Decl(noImplicitSymbolToString.ts, 2, 3))

let str = "hello ";
>str : Symbol(str, Decl(noImplicitSymbolToString.ts, 3, 3))

const templateStr = `hello ${symbol}`;
>templateStr : Symbol(templateStr, Decl(noImplicitSymbolToString.ts, 5, 5))
>symbol : Symbol(symbol, Decl(noImplicitSymbolToString.ts, 2, 3))

const appendStr = "hello " + symbol;
>appendStr : Symbol(appendStr, Decl(noImplicitSymbolToString.ts, 6, 5))
>symbol : Symbol(symbol, Decl(noImplicitSymbolToString.ts, 2, 3))

str += symbol;
>str : Symbol(str, Decl(noImplicitSymbolToString.ts, 3, 3))
>symbol : Symbol(symbol, Decl(noImplicitSymbolToString.ts, 2, 3))

26 changes: 26 additions & 0 deletions tests/baselines/reference/noImplicitSymbolToString.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
=== tests/cases/compiler/noImplicitSymbolToString.ts ===
// Fix #19666

let symbol!: symbol;
>symbol : symbol

let str = "hello ";
>str : string
>"hello " : "hello "

const templateStr = `hello ${symbol}`;
>templateStr : string
>`hello ${symbol}` : string
>symbol : symbol

const appendStr = "hello " + symbol;
>appendStr : string
>"hello " + symbol : string
>"hello " : "hello "
>symbol : symbol

str += symbol;
>str += symbol : string
>str : string
>symbol : symbol

8 changes: 8 additions & 0 deletions tests/cases/compiler/noImplicitSymbolToString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Fix #19666

let symbol!: symbol;
let str = "hello ";

const templateStr = `hello ${symbol}`;
const appendStr = "hello " + symbol;
str += symbol;

0 comments on commit fc08ff5

Please sign in to comment.