Skip to content

Commit

Permalink
allow narrowing for any left-hand in operand
Browse files Browse the repository at this point in the history
  • Loading branch information
gabritto committed Jul 22, 2021
1 parent 7c4c0a3 commit c14d573
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -916,8 +916,8 @@ namespace ts {
return isTypeOfExpression(expr1) && isNarrowableOperand(expr1.expression) && isStringLiteralLike(expr2);
}

function isNarrowableInOperands(left: Expression, right: Expression) {
return isNarrowingExpression(right) && (isIdentifier(left) || isStringLiteralLike(left));
function isNarrowableInRightOperand(expr: Expression) {
return isNarrowingExpression(expr);
}

function isNarrowingBinaryExpression(expr: BinaryExpression) {
Expand All @@ -936,7 +936,7 @@ namespace ts {
case SyntaxKind.InstanceOfKeyword:
return isNarrowableOperand(expr.left);
case SyntaxKind.InKeyword:
return isNarrowableInOperands(expr.left, expr.right);
return isNarrowableInRightOperand(expr.right);
case SyntaxKind.CommaToken:
return isNarrowingExpression(expr.right);
}
Expand Down
37 changes: 37 additions & 0 deletions tests/baselines/reference/controlFlowForInStatement2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//// [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`
}


//// [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`
}
48 changes: 48 additions & 0 deletions tests/baselines/reference/controlFlowForInStatement2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
=== 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))
}

55 changes: 55 additions & 0 deletions tests/baselines/reference/controlFlowForInStatement2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
=== 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
}

21 changes: 21 additions & 0 deletions tests/cases/conformance/controlFlow/controlFlowForInStatement2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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`
}

0 comments on commit c14d573

Please sign in to comment.