Check for symbol types in template expressions

This commit is contained in:
Jack Williams 2018-03-01 03:07:53 +00:00
parent c12fc0d6c3
commit fc08ff5a36
6 changed files with 97 additions and 1 deletions

View File

@ -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;

View File

@ -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'.

View File

@ -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;

View File

@ -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))

View File

@ -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

View File

@ -0,0 +1,8 @@
// Fix #19666
let symbol!: symbol;
let str = "hello ";
const templateStr = `hello ${symbol}`;
const appendStr = "hello " + symbol;
str += symbol;