Skip to content

Commit fc08ff5

Browse files
committed
Check for symbol types in template expressions
1 parent c12fc0d commit fc08ff5

File tree

6 files changed

+97
-1
lines changed

6 files changed

+97
-1
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19361,7 +19361,9 @@ namespace ts {
1936119361
// A place where we actually *are* concerned with the expressions' types are
1936219362
// in tagged templates.
1936319363
forEach(node.templateSpans, templateSpan => {
19364-
checkExpression(templateSpan.expression);
19364+
if (maybeTypeOfKind(checkExpression(templateSpan.expression), TypeFlags.ESSymbolLike)) {
19365+
error(templateSpan.expression, Diagnostics.Type_0_cannot_be_converted_to_type_1, typeToString(esSymbolType), typeToString(stringType));
19366+
}
1936519367
});
1936619368

1936719369
return stringType;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
tests/cases/compiler/noImplicitSymbolToString.ts(6,30): error TS2352: Type 'symbol' cannot be converted to type 'string'.
2+
tests/cases/compiler/noImplicitSymbolToString.ts(7,30): error TS2469: The '+' operator cannot be applied to type 'symbol'.
3+
tests/cases/compiler/noImplicitSymbolToString.ts(8,8): error TS2469: The '+=' operator cannot be applied to type 'symbol'.
4+
5+
6+
==== tests/cases/compiler/noImplicitSymbolToString.ts (3 errors) ====
7+
// Fix #19666
8+
9+
let symbol!: symbol;
10+
let str = "hello ";
11+
12+
const templateStr = `hello ${symbol}`;
13+
~~~~~~
14+
!!! error TS2352: Type 'symbol' cannot be converted to type 'string'.
15+
const appendStr = "hello " + symbol;
16+
~~~~~~
17+
!!! error TS2469: The '+' operator cannot be applied to type 'symbol'.
18+
str += symbol;
19+
~~~~~~
20+
!!! error TS2469: The '+=' operator cannot be applied to type 'symbol'.
21+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [noImplicitSymbolToString.ts]
2+
// Fix #19666
3+
4+
let symbol!: symbol;
5+
let str = "hello ";
6+
7+
const templateStr = `hello ${symbol}`;
8+
const appendStr = "hello " + symbol;
9+
str += symbol;
10+
11+
12+
//// [noImplicitSymbolToString.js]
13+
// Fix #19666
14+
var symbol;
15+
var str = "hello ";
16+
var templateStr = "hello " + symbol;
17+
var appendStr = "hello " + symbol;
18+
str += symbol;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/compiler/noImplicitSymbolToString.ts ===
2+
// Fix #19666
3+
4+
let symbol!: symbol;
5+
>symbol : Symbol(symbol, Decl(noImplicitSymbolToString.ts, 2, 3))
6+
7+
let str = "hello ";
8+
>str : Symbol(str, Decl(noImplicitSymbolToString.ts, 3, 3))
9+
10+
const templateStr = `hello ${symbol}`;
11+
>templateStr : Symbol(templateStr, Decl(noImplicitSymbolToString.ts, 5, 5))
12+
>symbol : Symbol(symbol, Decl(noImplicitSymbolToString.ts, 2, 3))
13+
14+
const appendStr = "hello " + symbol;
15+
>appendStr : Symbol(appendStr, Decl(noImplicitSymbolToString.ts, 6, 5))
16+
>symbol : Symbol(symbol, Decl(noImplicitSymbolToString.ts, 2, 3))
17+
18+
str += symbol;
19+
>str : Symbol(str, Decl(noImplicitSymbolToString.ts, 3, 3))
20+
>symbol : Symbol(symbol, Decl(noImplicitSymbolToString.ts, 2, 3))
21+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
=== tests/cases/compiler/noImplicitSymbolToString.ts ===
2+
// Fix #19666
3+
4+
let symbol!: symbol;
5+
>symbol : symbol
6+
7+
let str = "hello ";
8+
>str : string
9+
>"hello " : "hello "
10+
11+
const templateStr = `hello ${symbol}`;
12+
>templateStr : string
13+
>`hello ${symbol}` : string
14+
>symbol : symbol
15+
16+
const appendStr = "hello " + symbol;
17+
>appendStr : string
18+
>"hello " + symbol : string
19+
>"hello " : "hello "
20+
>symbol : symbol
21+
22+
str += symbol;
23+
>str += symbol : string
24+
>str : string
25+
>symbol : symbol
26+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Fix #19666
2+
3+
let symbol!: symbol;
4+
let str = "hello ";
5+
6+
const templateStr = `hello ${symbol}`;
7+
const appendStr = "hello " + symbol;
8+
str += symbol;

0 commit comments

Comments
 (0)