Skip to content

Commit 58781b0

Browse files
committed
allow type narrowing with NonNullExpression
1 parent a109b5d commit 58781b0

File tree

6 files changed

+58
-2
lines changed

6 files changed

+58
-2
lines changed

src/compiler/binder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,8 @@ namespace ts {
844844
case SyntaxKind.CallExpression:
845845
return hasNarrowableArgument(<CallExpression>expr);
846846
case SyntaxKind.ParenthesizedExpression:
847-
return isNarrowingExpression((<ParenthesizedExpression>expr).expression);
847+
case SyntaxKind.NonNullExpression:
848+
return isNarrowingExpression((<ParenthesizedExpression | NonNullExpression>expr).expression);
848849
case SyntaxKind.BinaryExpression:
849850
return isNarrowingBinaryExpression(<BinaryExpression>expr);
850851
case SyntaxKind.PrefixUnaryExpression:

src/compiler/checker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22554,7 +22554,8 @@ namespace ts {
2255422554
case SyntaxKind.CallExpression:
2255522555
return narrowTypeByCallExpression(type, <CallExpression>expr, assumeTrue);
2255622556
case SyntaxKind.ParenthesizedExpression:
22557-
return narrowType(type, (<ParenthesizedExpression>expr).expression, assumeTrue);
22557+
case SyntaxKind.NonNullExpression:
22558+
return narrowType(type, (<ParenthesizedExpression | NonNullExpression>expr).expression, assumeTrue);
2255822559
case SyntaxKind.BinaryExpression:
2255922560
return narrowTypeByBinaryExpression(type, <BinaryExpression>expr, assumeTrue);
2256022561
case SyntaxKind.PrefixUnaryExpression:
Lines changed: 10 additions & 0 deletions
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];
Lines changed: 14 additions & 0 deletions
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+
Lines changed: 27 additions & 0 deletions
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+
Lines changed: 3 additions & 0 deletions
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)