Skip to content

Commit 6a9bc38

Browse files
authored
Do not narrow using JSDoc type assertion nodes (microsoft#56048)
1 parent 65f33a2 commit 6a9bc38

5 files changed

+113
-0
lines changed

src/compiler/binder.ts

+5
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ import {
173173
isJSDocEnumTag,
174174
isJSDocTemplateTag,
175175
isJSDocTypeAlias,
176+
isJSDocTypeAssertion,
176177
isJsonSourceFile,
177178
isJsxNamespacedName,
178179
isLeftHandSideExpression,
@@ -1223,6 +1224,10 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
12231224
case SyntaxKind.CallExpression:
12241225
return hasNarrowableArgument(expr as CallExpression);
12251226
case SyntaxKind.ParenthesizedExpression:
1227+
if (isJSDocTypeAssertion(expr)) {
1228+
return false;
1229+
}
1230+
// fallthrough
12261231
case SyntaxKind.NonNullExpression:
12271232
return isNarrowingExpression((expr as ParenthesizedExpression | NonNullExpression).expression);
12281233
case SyntaxKind.BinaryExpression:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
index.js(12,8): error TS2678: Type '"invalid"' is not comparable to type '"foo" | "bar"'.
2+
3+
4+
==== index.js (1 errors) ====
5+
let value = "";
6+
7+
switch (/** @type {"foo" | "bar"} */ (value)) {
8+
case "bar":
9+
value;
10+
break;
11+
12+
case "foo":
13+
value;
14+
break;
15+
16+
case "invalid":
17+
~~~~~~~~~
18+
!!! error TS2678: Type '"invalid"' is not comparable to type '"foo" | "bar"'.
19+
value;
20+
break;
21+
}
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//// [tests/cases/compiler/parenthesizedJSDocCastDoesNotNarrow.ts] ////
2+
3+
=== index.js ===
4+
let value = "";
5+
>value : Symbol(value, Decl(index.js, 0, 3))
6+
7+
switch (/** @type {"foo" | "bar"} */ (value)) {
8+
>value : Symbol(value, Decl(index.js, 0, 3))
9+
10+
case "bar":
11+
value;
12+
>value : Symbol(value, Decl(index.js, 0, 3))
13+
14+
break;
15+
16+
case "foo":
17+
value;
18+
>value : Symbol(value, Decl(index.js, 0, 3))
19+
20+
break;
21+
22+
case "invalid":
23+
value;
24+
>value : Symbol(value, Decl(index.js, 0, 3))
25+
26+
break;
27+
}
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [tests/cases/compiler/parenthesizedJSDocCastDoesNotNarrow.ts] ////
2+
3+
=== index.js ===
4+
let value = "";
5+
>value : string
6+
>"" : ""
7+
8+
switch (/** @type {"foo" | "bar"} */ (value)) {
9+
>(value) : "foo" | "bar"
10+
>value : string
11+
12+
case "bar":
13+
>"bar" : "bar"
14+
15+
value;
16+
>value : string
17+
18+
break;
19+
20+
case "foo":
21+
>"foo" : "foo"
22+
23+
value;
24+
>value : string
25+
26+
break;
27+
28+
case "invalid":
29+
>"invalid" : "invalid"
30+
31+
value;
32+
>value : string
33+
34+
break;
35+
}
36+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// @strict: true
2+
// @noEmit: true
3+
// @checkJs: true
4+
// @allowJs: true
5+
6+
// @filename: index.js
7+
8+
let value = "";
9+
10+
switch (/** @type {"foo" | "bar"} */ (value)) {
11+
case "bar":
12+
value;
13+
break;
14+
15+
case "foo":
16+
value;
17+
break;
18+
19+
case "invalid":
20+
value;
21+
break;
22+
}

0 commit comments

Comments
 (0)