Merge pull request #41075 from uhyo/fix-36958

allow type narrowing with NonNullExpression
This commit is contained in:
Wesley Wigham 2020-10-27 20:10:27 -07:00 committed by GitHub
commit 6acce0ca6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 58 additions and 2 deletions

View File

@ -845,7 +845,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:

View File

@ -22618,7 +22618,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:

View File

@ -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];

View File

@ -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))

View File

@ -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

View File

@ -0,0 +1,3 @@
const m = ''.match('');
m! && m[0];
m?.[0]! && m[0];