Skip to content

Commit db0f793

Browse files
authored
Allow narrowing for any left-hand in operand (#45152)
* allow narrowing for any left-hand in operand
1 parent 366e9de commit db0f793

File tree

5 files changed

+189
-5
lines changed

5 files changed

+189
-5
lines changed

src/compiler/binder.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -916,10 +916,6 @@ namespace ts {
916916
return isTypeOfExpression(expr1) && isNarrowableOperand(expr1.expression) && isStringLiteralLike(expr2);
917917
}
918918

919-
function isNarrowableInOperands(left: Expression, right: Expression) {
920-
return isNarrowingExpression(right) && (isIdentifier(left) || isStringLiteralLike(left));
921-
}
922-
923919
function isNarrowingBinaryExpression(expr: BinaryExpression) {
924920
switch (expr.operatorToken.kind) {
925921
case SyntaxKind.EqualsToken:
@@ -936,7 +932,7 @@ namespace ts {
936932
case SyntaxKind.InstanceOfKeyword:
937933
return isNarrowableOperand(expr.left);
938934
case SyntaxKind.InKeyword:
939-
return isNarrowableInOperands(expr.left, expr.right);
935+
return isNarrowingExpression(expr.right);
940936
case SyntaxKind.CommaToken:
941937
return isNarrowingExpression(expr.right);
942938
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//// [controlFlowForInStatement2.ts]
2+
const keywordA = 'a';
3+
const keywordB = 'b';
4+
5+
type A = { [keywordA]: number };
6+
type B = { [keywordB]: string };
7+
8+
declare const c: A | B;
9+
10+
if ('a' in c) {
11+
c; // narrowed to `A`
12+
}
13+
14+
if (keywordA in c) {
15+
c; // also narrowed to `A`
16+
}
17+
18+
let stringB: string = 'b';
19+
20+
if ((stringB as 'b') in c) {
21+
c; // narrowed to `B`
22+
}
23+
24+
if ((stringB as ('a' | 'b')) in c) {
25+
c; // not narrowed
26+
}
27+
28+
//// [controlFlowForInStatement2.js]
29+
var keywordA = 'a';
30+
var keywordB = 'b';
31+
if ('a' in c) {
32+
c; // narrowed to `A`
33+
}
34+
if (keywordA in c) {
35+
c; // also narrowed to `A`
36+
}
37+
var stringB = 'b';
38+
if (stringB in c) {
39+
c; // narrowed to `B`
40+
}
41+
if (stringB in c) {
42+
c; // not narrowed
43+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
=== tests/cases/conformance/controlFlow/controlFlowForInStatement2.ts ===
2+
const keywordA = 'a';
3+
>keywordA : Symbol(keywordA, Decl(controlFlowForInStatement2.ts, 0, 5))
4+
5+
const keywordB = 'b';
6+
>keywordB : Symbol(keywordB, Decl(controlFlowForInStatement2.ts, 1, 5))
7+
8+
type A = { [keywordA]: number };
9+
>A : Symbol(A, Decl(controlFlowForInStatement2.ts, 1, 21))
10+
>[keywordA] : Symbol([keywordA], Decl(controlFlowForInStatement2.ts, 3, 10))
11+
>keywordA : Symbol(keywordA, Decl(controlFlowForInStatement2.ts, 0, 5))
12+
13+
type B = { [keywordB]: string };
14+
>B : Symbol(B, Decl(controlFlowForInStatement2.ts, 3, 32))
15+
>[keywordB] : Symbol([keywordB], Decl(controlFlowForInStatement2.ts, 4, 10))
16+
>keywordB : Symbol(keywordB, Decl(controlFlowForInStatement2.ts, 1, 5))
17+
18+
declare const c: A | B;
19+
>c : Symbol(c, Decl(controlFlowForInStatement2.ts, 6, 13))
20+
>A : Symbol(A, Decl(controlFlowForInStatement2.ts, 1, 21))
21+
>B : Symbol(B, Decl(controlFlowForInStatement2.ts, 3, 32))
22+
23+
if ('a' in c) {
24+
>c : Symbol(c, Decl(controlFlowForInStatement2.ts, 6, 13))
25+
26+
c; // narrowed to `A`
27+
>c : Symbol(c, Decl(controlFlowForInStatement2.ts, 6, 13))
28+
}
29+
30+
if (keywordA in c) {
31+
>keywordA : Symbol(keywordA, Decl(controlFlowForInStatement2.ts, 0, 5))
32+
>c : Symbol(c, Decl(controlFlowForInStatement2.ts, 6, 13))
33+
34+
c; // also narrowed to `A`
35+
>c : Symbol(c, Decl(controlFlowForInStatement2.ts, 6, 13))
36+
}
37+
38+
let stringB: string = 'b';
39+
>stringB : Symbol(stringB, Decl(controlFlowForInStatement2.ts, 16, 3))
40+
41+
if ((stringB as 'b') in c) {
42+
>stringB : Symbol(stringB, Decl(controlFlowForInStatement2.ts, 16, 3))
43+
>c : Symbol(c, Decl(controlFlowForInStatement2.ts, 6, 13))
44+
45+
c; // narrowed to `B`
46+
>c : Symbol(c, Decl(controlFlowForInStatement2.ts, 6, 13))
47+
}
48+
49+
if ((stringB as ('a' | 'b')) in c) {
50+
>stringB : Symbol(stringB, Decl(controlFlowForInStatement2.ts, 16, 3))
51+
>c : Symbol(c, Decl(controlFlowForInStatement2.ts, 6, 13))
52+
53+
c; // not narrowed
54+
>c : Symbol(c, Decl(controlFlowForInStatement2.ts, 6, 13))
55+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
=== tests/cases/conformance/controlFlow/controlFlowForInStatement2.ts ===
2+
const keywordA = 'a';
3+
>keywordA : "a"
4+
>'a' : "a"
5+
6+
const keywordB = 'b';
7+
>keywordB : "b"
8+
>'b' : "b"
9+
10+
type A = { [keywordA]: number };
11+
>A : A
12+
>[keywordA] : number
13+
>keywordA : "a"
14+
15+
type B = { [keywordB]: string };
16+
>B : B
17+
>[keywordB] : string
18+
>keywordB : "b"
19+
20+
declare const c: A | B;
21+
>c : A | B
22+
23+
if ('a' in c) {
24+
>'a' in c : boolean
25+
>'a' : "a"
26+
>c : A | B
27+
28+
c; // narrowed to `A`
29+
>c : A
30+
}
31+
32+
if (keywordA in c) {
33+
>keywordA in c : boolean
34+
>keywordA : "a"
35+
>c : A | B
36+
37+
c; // also narrowed to `A`
38+
>c : A
39+
}
40+
41+
let stringB: string = 'b';
42+
>stringB : string
43+
>'b' : "b"
44+
45+
if ((stringB as 'b') in c) {
46+
>(stringB as 'b') in c : boolean
47+
>(stringB as 'b') : "b"
48+
>stringB as 'b' : "b"
49+
>stringB : string
50+
>c : A | B
51+
52+
c; // narrowed to `B`
53+
>c : B
54+
}
55+
56+
if ((stringB as ('a' | 'b')) in c) {
57+
>(stringB as ('a' | 'b')) in c : boolean
58+
>(stringB as ('a' | 'b')) : "a" | "b"
59+
>stringB as ('a' | 'b') : "a" | "b"
60+
>stringB : string
61+
>c : A | B
62+
63+
c; // not narrowed
64+
>c : A | B
65+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const keywordA = 'a';
2+
const keywordB = 'b';
3+
4+
type A = { [keywordA]: number };
5+
type B = { [keywordB]: string };
6+
7+
declare const c: A | B;
8+
9+
if ('a' in c) {
10+
c; // narrowed to `A`
11+
}
12+
13+
if (keywordA in c) {
14+
c; // also narrowed to `A`
15+
}
16+
17+
let stringB: string = 'b';
18+
19+
if ((stringB as 'b') in c) {
20+
c; // narrowed to `B`
21+
}
22+
23+
if ((stringB as ('a' | 'b')) in c) {
24+
c; // not narrowed
25+
}

0 commit comments

Comments
 (0)