Skip to content

allow type narrowing with NonNullExpression #41075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,8 @@ namespace ts {
case SyntaxKind.CallExpression:
return hasNarrowableArgument(<CallExpression>expr);
case SyntaxKind.ParenthesizedExpression:
return isNarrowingExpression((<ParenthesizedExpression>expr).expression);
case SyntaxKind.NonNullExpression:
return isNarrowingExpression((<ParenthesizedExpression | NonNullExpression>expr).expression);
case SyntaxKind.BinaryExpression:
return isNarrowingBinaryExpression(<BinaryExpression>expr);
case SyntaxKind.PrefixUnaryExpression:
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22554,7 +22554,8 @@ namespace ts {
case SyntaxKind.CallExpression:
return narrowTypeByCallExpression(type, <CallExpression>expr, assumeTrue);
case SyntaxKind.ParenthesizedExpression:
return narrowType(type, (<ParenthesizedExpression>expr).expression, assumeTrue);
case SyntaxKind.NonNullExpression:
return narrowType(type, (<ParenthesizedExpression | NonNullExpression>expr).expression, assumeTrue);
case SyntaxKind.BinaryExpression:
return narrowTypeByBinaryExpression(type, <BinaryExpression>expr, assumeTrue);
case SyntaxKind.PrefixUnaryExpression:
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/narrowingWithNonNullExpression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [narrowingWithNonNullExpression.ts]
const m = ''.match('');
m! && m[0];
m?.[0]! && m[0];


//// [narrowingWithNonNullExpression.js]
var m = ''.match('');
m && m[0];
(m === null || m === void 0 ? void 0 : m[0]) && m[0];
14 changes: 14 additions & 0 deletions tests/baselines/reference/narrowingWithNonNullExpression.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== tests/cases/compiler/narrowingWithNonNullExpression.ts ===
const m = ''.match('');
>m : Symbol(m, Decl(narrowingWithNonNullExpression.ts, 0, 5))
>''.match : Symbol(String.match, Decl(lib.es5.d.ts, --, --))
>match : Symbol(String.match, Decl(lib.es5.d.ts, --, --))

m! && m[0];
>m : Symbol(m, Decl(narrowingWithNonNullExpression.ts, 0, 5))
>m : Symbol(m, Decl(narrowingWithNonNullExpression.ts, 0, 5))

m?.[0]! && m[0];
>m : Symbol(m, Decl(narrowingWithNonNullExpression.ts, 0, 5))
>m : Symbol(m, Decl(narrowingWithNonNullExpression.ts, 0, 5))

27 changes: 27 additions & 0 deletions tests/baselines/reference/narrowingWithNonNullExpression.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/compiler/narrowingWithNonNullExpression.ts ===
const m = ''.match('');
>m : RegExpMatchArray
>''.match('') : RegExpMatchArray
>''.match : (regexp: string | RegExp) => RegExpMatchArray
>'' : ""
>match : (regexp: string | RegExp) => RegExpMatchArray
>'' : ""

m! && m[0];
>m! && m[0] : string
>m! : RegExpMatchArray
>m : RegExpMatchArray
>m[0] : string
>m : RegExpMatchArray
>0 : 0

m?.[0]! && m[0];
>m?.[0]! && m[0] : string
>m?.[0]! : string
>m?.[0] : string
>m : RegExpMatchArray
>0 : 0
>m[0] : string
>m : RegExpMatchArray
>0 : 0

3 changes: 3 additions & 0 deletions tests/cases/compiler/narrowingWithNonNullExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const m = ''.match('');
m! && m[0];
m?.[0]! && m[0];