Allow narrowing for any left-hand in operand (#45152)

* allow narrowing for any left-hand in operand
This commit is contained in:
Gabriela Araujo Britto
2021-07-29 14:06:45 -07:00
committed by GitHub
parent 366e9de264
commit db0f7938dd
5 changed files with 189 additions and 5 deletions

View File

@@ -916,10 +916,6 @@ namespace ts {
return isTypeOfExpression(expr1) && isNarrowableOperand(expr1.expression) && isStringLiteralLike(expr2);
}
function isNarrowableInOperands(left: Expression, right: Expression) {
return isNarrowingExpression(right) && (isIdentifier(left) || isStringLiteralLike(left));
}
function isNarrowingBinaryExpression(expr: BinaryExpression) {
switch (expr.operatorToken.kind) {
case SyntaxKind.EqualsToken:
@@ -936,7 +932,7 @@ namespace ts {
case SyntaxKind.InstanceOfKeyword:
return isNarrowableOperand(expr.left);
case SyntaxKind.InKeyword:
return isNarrowableInOperands(expr.left, expr.right);
return isNarrowingExpression(expr.right);
case SyntaxKind.CommaToken:
return isNarrowingExpression(expr.right);
}

View File

@@ -0,0 +1,43 @@
//// [controlFlowForInStatement2.ts]
const keywordA = 'a';
const keywordB = 'b';
type A = { [keywordA]: number };
type B = { [keywordB]: string };
declare const c: A | B;
if ('a' in c) {
c; // narrowed to `A`
}
if (keywordA in c) {
c; // also narrowed to `A`
}
let stringB: string = 'b';
if ((stringB as 'b') in c) {
c; // narrowed to `B`
}
if ((stringB as ('a' | 'b')) in c) {
c; // not narrowed
}
//// [controlFlowForInStatement2.js]
var keywordA = 'a';
var keywordB = 'b';
if ('a' in c) {
c; // narrowed to `A`
}
if (keywordA in c) {
c; // also narrowed to `A`
}
var stringB = 'b';
if (stringB in c) {
c; // narrowed to `B`
}
if (stringB in c) {
c; // not narrowed
}

View File

@@ -0,0 +1,55 @@
=== tests/cases/conformance/controlFlow/controlFlowForInStatement2.ts ===
const keywordA = 'a';
>keywordA : Symbol(keywordA, Decl(controlFlowForInStatement2.ts, 0, 5))
const keywordB = 'b';
>keywordB : Symbol(keywordB, Decl(controlFlowForInStatement2.ts, 1, 5))
type A = { [keywordA]: number };
>A : Symbol(A, Decl(controlFlowForInStatement2.ts, 1, 21))
>[keywordA] : Symbol([keywordA], Decl(controlFlowForInStatement2.ts, 3, 10))
>keywordA : Symbol(keywordA, Decl(controlFlowForInStatement2.ts, 0, 5))
type B = { [keywordB]: string };
>B : Symbol(B, Decl(controlFlowForInStatement2.ts, 3, 32))
>[keywordB] : Symbol([keywordB], Decl(controlFlowForInStatement2.ts, 4, 10))
>keywordB : Symbol(keywordB, Decl(controlFlowForInStatement2.ts, 1, 5))
declare const c: A | B;
>c : Symbol(c, Decl(controlFlowForInStatement2.ts, 6, 13))
>A : Symbol(A, Decl(controlFlowForInStatement2.ts, 1, 21))
>B : Symbol(B, Decl(controlFlowForInStatement2.ts, 3, 32))
if ('a' in c) {
>c : Symbol(c, Decl(controlFlowForInStatement2.ts, 6, 13))
c; // narrowed to `A`
>c : Symbol(c, Decl(controlFlowForInStatement2.ts, 6, 13))
}
if (keywordA in c) {
>keywordA : Symbol(keywordA, Decl(controlFlowForInStatement2.ts, 0, 5))
>c : Symbol(c, Decl(controlFlowForInStatement2.ts, 6, 13))
c; // also narrowed to `A`
>c : Symbol(c, Decl(controlFlowForInStatement2.ts, 6, 13))
}
let stringB: string = 'b';
>stringB : Symbol(stringB, Decl(controlFlowForInStatement2.ts, 16, 3))
if ((stringB as 'b') in c) {
>stringB : Symbol(stringB, Decl(controlFlowForInStatement2.ts, 16, 3))
>c : Symbol(c, Decl(controlFlowForInStatement2.ts, 6, 13))
c; // narrowed to `B`
>c : Symbol(c, Decl(controlFlowForInStatement2.ts, 6, 13))
}
if ((stringB as ('a' | 'b')) in c) {
>stringB : Symbol(stringB, Decl(controlFlowForInStatement2.ts, 16, 3))
>c : Symbol(c, Decl(controlFlowForInStatement2.ts, 6, 13))
c; // not narrowed
>c : Symbol(c, Decl(controlFlowForInStatement2.ts, 6, 13))
}

View File

@@ -0,0 +1,65 @@
=== tests/cases/conformance/controlFlow/controlFlowForInStatement2.ts ===
const keywordA = 'a';
>keywordA : "a"
>'a' : "a"
const keywordB = 'b';
>keywordB : "b"
>'b' : "b"
type A = { [keywordA]: number };
>A : A
>[keywordA] : number
>keywordA : "a"
type B = { [keywordB]: string };
>B : B
>[keywordB] : string
>keywordB : "b"
declare const c: A | B;
>c : A | B
if ('a' in c) {
>'a' in c : boolean
>'a' : "a"
>c : A | B
c; // narrowed to `A`
>c : A
}
if (keywordA in c) {
>keywordA in c : boolean
>keywordA : "a"
>c : A | B
c; // also narrowed to `A`
>c : A
}
let stringB: string = 'b';
>stringB : string
>'b' : "b"
if ((stringB as 'b') in c) {
>(stringB as 'b') in c : boolean
>(stringB as 'b') : "b"
>stringB as 'b' : "b"
>stringB : string
>c : A | B
c; // narrowed to `B`
>c : B
}
if ((stringB as ('a' | 'b')) in c) {
>(stringB as ('a' | 'b')) in c : boolean
>(stringB as ('a' | 'b')) : "a" | "b"
>stringB as ('a' | 'b') : "a" | "b"
>stringB : string
>c : A | B
c; // not narrowed
>c : A | B
}

View File

@@ -0,0 +1,25 @@
const keywordA = 'a';
const keywordB = 'b';
type A = { [keywordA]: number };
type B = { [keywordB]: string };
declare const c: A | B;
if ('a' in c) {
c; // narrowed to `A`
}
if (keywordA in c) {
c; // also narrowed to `A`
}
let stringB: string = 'b';
if ((stringB as 'b') in c) {
c; // narrowed to `B`
}
if ((stringB as ('a' | 'b')) in c) {
c; // not narrowed
}