-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow narrowing for any left-hand in operand (#45152)
* allow narrowing for any left-hand in operand
- Loading branch information
Showing
5 changed files
with
189 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
55 changes: 55 additions & 0 deletions
55
tests/baselines/reference/controlFlowForInStatement2.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
65 changes: 65 additions & 0 deletions
65
tests/baselines/reference/controlFlowForInStatement2.types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/cases/conformance/controlFlow/controlFlowForInStatement2.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |