Skip to content

Commit 9fe6c36

Browse files
authored
Fixed syntactic nullisness semantics for tagged template expressions (#60616)
1 parent 96410eb commit 9fe6c36

File tree

6 files changed

+117
-2
lines changed

6 files changed

+117
-2
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39927,6 +39927,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3992739927
switch (node.kind) {
3992839928
case SyntaxKind.AwaitExpression:
3992939929
case SyntaxKind.CallExpression:
39930+
case SyntaxKind.TaggedTemplateExpression:
3993039931
case SyntaxKind.ElementAccessExpression:
3993139932
case SyntaxKind.MetaProperty:
3993239933
case SyntaxKind.NewExpression:

tests/baselines/reference/predicateSemantics.errors.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ predicateSemantics.ts(36,8): error TS2872: This kind of expression is always tru
1212
predicateSemantics.ts(51,14): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish.
1313
predicateSemantics.ts(52,14): error TS2695: Left side of comma operator is unused and has no side effects.
1414
predicateSemantics.ts(52,14): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish.
15+
predicateSemantics.ts(70,1): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish.
16+
predicateSemantics.ts(71,1): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish.
1517

1618

17-
==== predicateSemantics.ts (14 errors) ====
19+
==== predicateSemantics.ts (16 errors) ====
1820
declare let cond: any;
1921

2022
// OK: One or other operand is possibly nullish
@@ -103,4 +105,19 @@ predicateSemantics.ts(52,14): error TS2869: Right operand of ?? is unreachable b
103105
const p = new.target ?? 32;
104106
}
105107
}
108+
109+
// https://github.com/microsoft/TypeScript/issues/60614
110+
declare function tag<T>(
111+
strings: TemplateStringsArray,
112+
...values: number[]
113+
): T | null;
114+
115+
tag`foo${1}` ?? 32; // ok
116+
117+
`foo${1}` ?? 32; // error
118+
~~~~~~~~~
119+
!!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish.
120+
`foo` ?? 32; // error
121+
~~~~~
122+
!!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish.
106123

tests/baselines/reference/predicateSemantics.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,25 @@ class X {
6161
const p = new.target ?? 32;
6262
}
6363
}
64+
65+
// https://github.com/microsoft/TypeScript/issues/60614
66+
declare function tag<T>(
67+
strings: TemplateStringsArray,
68+
...values: number[]
69+
): T | null;
70+
71+
tag`foo${1}` ?? 32; // ok
72+
73+
`foo${1}` ?? 32; // error
74+
`foo` ?? 32; // error
6475

6576

6677
//// [predicateSemantics.js]
67-
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
78+
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
79+
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
80+
return cooked;
81+
};
82+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
6883
// OK: One or other operand is possibly nullish
6984
var test1 = (_a = (cond ? undefined : 32)) !== null && _a !== void 0 ? _a : "possibly reached";
7085
// Not OK: Both operands nullish
@@ -122,3 +137,6 @@ var X = /** @class */ (function () {
122137
}
123138
return X;
124139
}());
140+
(_k = tag(__makeTemplateObject(["foo", ""], ["foo", ""]), 1)) !== null && _k !== void 0 ? _k : 32; // ok
141+
(_l = "foo".concat(1)) !== null && _l !== void 0 ? _l : 32; // error
142+
"foo" !== null && "foo" !== void 0 ? "foo" : 32; // error

tests/baselines/reference/predicateSemantics.symbols

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,24 @@ class X {
116116
}
117117
}
118118

119+
// https://github.com/microsoft/TypeScript/issues/60614
120+
declare function tag<T>(
121+
>tag : Symbol(tag, Decl(predicateSemantics.ts, 59, 1))
122+
>T : Symbol(T, Decl(predicateSemantics.ts, 62, 21))
123+
124+
strings: TemplateStringsArray,
125+
>strings : Symbol(strings, Decl(predicateSemantics.ts, 62, 24))
126+
>TemplateStringsArray : Symbol(TemplateStringsArray, Decl(lib.es5.d.ts, --, --))
127+
128+
...values: number[]
129+
>values : Symbol(values, Decl(predicateSemantics.ts, 63, 32))
130+
131+
): T | null;
132+
>T : Symbol(T, Decl(predicateSemantics.ts, 62, 21))
133+
134+
tag`foo${1}` ?? 32; // ok
135+
>tag : Symbol(tag, Decl(predicateSemantics.ts, 59, 1))
136+
137+
`foo${1}` ?? 32; // error
138+
`foo` ?? 32; // error
139+

tests/baselines/reference/predicateSemantics.types

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,50 @@ class X {
328328
}
329329
}
330330

331+
// https://github.com/microsoft/TypeScript/issues/60614
332+
declare function tag<T>(
333+
>tag : <T>(strings: TemplateStringsArray, ...values: number[]) => T | null
334+
> : ^ ^^ ^^ ^^^^^ ^^ ^^^^^
335+
336+
strings: TemplateStringsArray,
337+
>strings : TemplateStringsArray
338+
> : ^^^^^^^^^^^^^^^^^^^^
339+
340+
...values: number[]
341+
>values : number[]
342+
> : ^^^^^^^^
343+
344+
): T | null;
345+
346+
tag`foo${1}` ?? 32; // ok
347+
>tag`foo${1}` ?? 32 : unknown
348+
> : ^^^^^^^
349+
>tag`foo${1}` : unknown
350+
> : ^^^^^^^
351+
>tag : <T>(strings: TemplateStringsArray, ...values: number[]) => T | null
352+
> : ^ ^^ ^^ ^^^^^ ^^ ^^^^^
353+
>`foo${1}` : string
354+
> : ^^^^^^
355+
>1 : 1
356+
> : ^
357+
>32 : 32
358+
> : ^^
359+
360+
`foo${1}` ?? 32; // error
361+
>`foo${1}` ?? 32 : 32 | "foo1"
362+
> : ^^^^^^^^^^^
363+
>`foo${1}` : "foo1"
364+
> : ^^^^^^
365+
>1 : 1
366+
> : ^
367+
>32 : 32
368+
> : ^^
369+
370+
`foo` ?? 32; // error
371+
>`foo` ?? 32 : 32 | "foo"
372+
> : ^^^^^^^^^^
373+
>`foo` : "foo"
374+
> : ^^^^^
375+
>32 : 32
376+
> : ^^
377+

tests/cases/compiler/predicateSemantics.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,14 @@ class X {
5858
const p = new.target ?? 32;
5959
}
6060
}
61+
62+
// https://github.com/microsoft/TypeScript/issues/60614
63+
declare function tag<T>(
64+
strings: TemplateStringsArray,
65+
...values: number[]
66+
): T | null;
67+
68+
tag`foo${1}` ?? 32; // ok
69+
70+
`foo${1}` ?? 32; // error
71+
`foo` ?? 32; // error

0 commit comments

Comments
 (0)