mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 08:11:30 -06:00
Merge pull request #41075 from uhyo/fix-36958
allow type narrowing with NonNullExpression
This commit is contained in:
commit
6acce0ca6f
@ -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:
|
||||
|
||||
@ -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:
|
||||
|
||||
10
tests/baselines/reference/narrowingWithNonNullExpression.js
Normal file
10
tests/baselines/reference/narrowingWithNonNullExpression.js
Normal 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];
|
||||
@ -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))
|
||||
|
||||
@ -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
tests/cases/compiler/narrowingWithNonNullExpression.ts
Normal file
3
tests/cases/compiler/narrowingWithNonNullExpression.ts
Normal file
@ -0,0 +1,3 @@
|
||||
const m = ''.match('');
|
||||
m! && m[0];
|
||||
m?.[0]! && m[0];
|
||||
Loading…
x
Reference in New Issue
Block a user