Skip to content

Commit 6acce0c

Browse files
authored
Merge pull request microsoft#41075 from uhyo/fix-36958
allow type narrowing with NonNullExpression
2 parents af38ab9 + 58781b0 commit 6acce0c

File tree

6 files changed

+58
-2
lines changed

6 files changed

+58
-2
lines changed

src/compiler/binder.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,8 @@ namespace ts {
845845
case SyntaxKind.CallExpression:
846846
return hasNarrowableArgument(<CallExpression>expr);
847847
case SyntaxKind.ParenthesizedExpression:
848-
return isNarrowingExpression((<ParenthesizedExpression>expr).expression);
848+
case SyntaxKind.NonNullExpression:
849+
return isNarrowingExpression((<ParenthesizedExpression | NonNullExpression>expr).expression);
849850
case SyntaxKind.BinaryExpression:
850851
return isNarrowingBinaryExpression(<BinaryExpression>expr);
851852
case SyntaxKind.PrefixUnaryExpression:

src/compiler/checker.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -22618,7 +22618,8 @@ namespace ts {
2261822618
case SyntaxKind.CallExpression:
2261922619
return narrowTypeByCallExpression(type, <CallExpression>expr, assumeTrue);
2262022620
case SyntaxKind.ParenthesizedExpression:
22621-
return narrowType(type, (<ParenthesizedExpression>expr).expression, assumeTrue);
22621+
case SyntaxKind.NonNullExpression:
22622+
return narrowType(type, (<ParenthesizedExpression | NonNullExpression>expr).expression, assumeTrue);
2262222623
case SyntaxKind.BinaryExpression:
2262322624
return narrowTypeByBinaryExpression(type, <BinaryExpression>expr, assumeTrue);
2262422625
case SyntaxKind.PrefixUnaryExpression:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [narrowingWithNonNullExpression.ts]
2+
const m = ''.match('');
3+
m! && m[0];
4+
m?.[0]! && m[0];
5+
6+
7+
//// [narrowingWithNonNullExpression.js]
8+
var m = ''.match('');
9+
m && m[0];
10+
(m === null || m === void 0 ? void 0 : m[0]) && m[0];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/compiler/narrowingWithNonNullExpression.ts ===
2+
const m = ''.match('');
3+
>m : Symbol(m, Decl(narrowingWithNonNullExpression.ts, 0, 5))
4+
>''.match : Symbol(String.match, Decl(lib.es5.d.ts, --, --))
5+
>match : Symbol(String.match, Decl(lib.es5.d.ts, --, --))
6+
7+
m! && m[0];
8+
>m : Symbol(m, Decl(narrowingWithNonNullExpression.ts, 0, 5))
9+
>m : Symbol(m, Decl(narrowingWithNonNullExpression.ts, 0, 5))
10+
11+
m?.[0]! && m[0];
12+
>m : Symbol(m, Decl(narrowingWithNonNullExpression.ts, 0, 5))
13+
>m : Symbol(m, Decl(narrowingWithNonNullExpression.ts, 0, 5))
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
=== tests/cases/compiler/narrowingWithNonNullExpression.ts ===
2+
const m = ''.match('');
3+
>m : RegExpMatchArray
4+
>''.match('') : RegExpMatchArray
5+
>''.match : (regexp: string | RegExp) => RegExpMatchArray
6+
>'' : ""
7+
>match : (regexp: string | RegExp) => RegExpMatchArray
8+
>'' : ""
9+
10+
m! && m[0];
11+
>m! && m[0] : string
12+
>m! : RegExpMatchArray
13+
>m : RegExpMatchArray
14+
>m[0] : string
15+
>m : RegExpMatchArray
16+
>0 : 0
17+
18+
m?.[0]! && m[0];
19+
>m?.[0]! && m[0] : string
20+
>m?.[0]! : string
21+
>m?.[0] : string
22+
>m : RegExpMatchArray
23+
>0 : 0
24+
>m[0] : string
25+
>m : RegExpMatchArray
26+
>0 : 0
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const m = ''.match('');
2+
m! && m[0];
3+
m?.[0]! && m[0];

0 commit comments

Comments
 (0)