-
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.
feat(42639): allow narrowing type in 'in' operator with the identifie…
…r on the left side (#44893)
- Loading branch information
1 parent
1e2c77e
commit e064817
Showing
6 changed files
with
216 additions
and
7 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
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,47 @@ | ||
//// [controlFlowInOperator.ts] | ||
const a = 'a'; | ||
const b = 'b'; | ||
const d = 'd'; | ||
|
||
type A = { [a]: number; }; | ||
type B = { [b]: string; }; | ||
|
||
declare const c: A | B; | ||
|
||
if ('a' in c) { | ||
c; // A | ||
c['a']; // number; | ||
} | ||
|
||
if ('d' in c) { | ||
c; // never | ||
} | ||
|
||
if (a in c) { | ||
c; // A | ||
c[a]; // number; | ||
} | ||
|
||
if (d in c) { | ||
c; // never | ||
} | ||
|
||
|
||
//// [controlFlowInOperator.js] | ||
var a = 'a'; | ||
var b = 'b'; | ||
var d = 'd'; | ||
if ('a' in c) { | ||
c; // A | ||
c['a']; // number; | ||
} | ||
if ('d' in c) { | ||
c; // never | ||
} | ||
if (a in c) { | ||
c; // A | ||
c[a]; // number; | ||
} | ||
if (d in c) { | ||
c; // never | ||
} |
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,63 @@ | ||
=== tests/cases/conformance/controlFlow/controlFlowInOperator.ts === | ||
const a = 'a'; | ||
>a : Symbol(a, Decl(controlFlowInOperator.ts, 0, 5)) | ||
|
||
const b = 'b'; | ||
>b : Symbol(b, Decl(controlFlowInOperator.ts, 1, 5)) | ||
|
||
const d = 'd'; | ||
>d : Symbol(d, Decl(controlFlowInOperator.ts, 2, 5)) | ||
|
||
type A = { [a]: number; }; | ||
>A : Symbol(A, Decl(controlFlowInOperator.ts, 2, 14)) | ||
>[a] : Symbol([a], Decl(controlFlowInOperator.ts, 4, 10)) | ||
>a : Symbol(a, Decl(controlFlowInOperator.ts, 0, 5)) | ||
|
||
type B = { [b]: string; }; | ||
>B : Symbol(B, Decl(controlFlowInOperator.ts, 4, 26)) | ||
>[b] : Symbol([b], Decl(controlFlowInOperator.ts, 5, 10)) | ||
>b : Symbol(b, Decl(controlFlowInOperator.ts, 1, 5)) | ||
|
||
declare const c: A | B; | ||
>c : Symbol(c, Decl(controlFlowInOperator.ts, 7, 13)) | ||
>A : Symbol(A, Decl(controlFlowInOperator.ts, 2, 14)) | ||
>B : Symbol(B, Decl(controlFlowInOperator.ts, 4, 26)) | ||
|
||
if ('a' in c) { | ||
>c : Symbol(c, Decl(controlFlowInOperator.ts, 7, 13)) | ||
|
||
c; // A | ||
>c : Symbol(c, Decl(controlFlowInOperator.ts, 7, 13)) | ||
|
||
c['a']; // number; | ||
>c : Symbol(c, Decl(controlFlowInOperator.ts, 7, 13)) | ||
>'a' : Symbol([a], Decl(controlFlowInOperator.ts, 4, 10)) | ||
} | ||
|
||
if ('d' in c) { | ||
>c : Symbol(c, Decl(controlFlowInOperator.ts, 7, 13)) | ||
|
||
c; // never | ||
>c : Symbol(c, Decl(controlFlowInOperator.ts, 7, 13)) | ||
} | ||
|
||
if (a in c) { | ||
>a : Symbol(a, Decl(controlFlowInOperator.ts, 0, 5)) | ||
>c : Symbol(c, Decl(controlFlowInOperator.ts, 7, 13)) | ||
|
||
c; // A | ||
>c : Symbol(c, Decl(controlFlowInOperator.ts, 7, 13)) | ||
|
||
c[a]; // number; | ||
>c : Symbol(c, Decl(controlFlowInOperator.ts, 7, 13)) | ||
>a : Symbol(a, Decl(controlFlowInOperator.ts, 0, 5)) | ||
} | ||
|
||
if (d in c) { | ||
>d : Symbol(d, Decl(controlFlowInOperator.ts, 2, 5)) | ||
>c : Symbol(c, Decl(controlFlowInOperator.ts, 7, 13)) | ||
|
||
c; // never | ||
>c : Symbol(c, Decl(controlFlowInOperator.ts, 7, 13)) | ||
} | ||
|
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,72 @@ | ||
=== tests/cases/conformance/controlFlow/controlFlowInOperator.ts === | ||
const a = 'a'; | ||
>a : "a" | ||
>'a' : "a" | ||
|
||
const b = 'b'; | ||
>b : "b" | ||
>'b' : "b" | ||
|
||
const d = 'd'; | ||
>d : "d" | ||
>'d' : "d" | ||
|
||
type A = { [a]: number; }; | ||
>A : A | ||
>[a] : number | ||
>a : "a" | ||
|
||
type B = { [b]: string; }; | ||
>B : B | ||
>[b] : string | ||
>b : "b" | ||
|
||
declare const c: A | B; | ||
>c : A | B | ||
|
||
if ('a' in c) { | ||
>'a' in c : boolean | ||
>'a' : "a" | ||
>c : A | B | ||
|
||
c; // A | ||
>c : A | ||
|
||
c['a']; // number; | ||
>c['a'] : number | ||
>c : A | ||
>'a' : "a" | ||
} | ||
|
||
if ('d' in c) { | ||
>'d' in c : boolean | ||
>'d' : "d" | ||
>c : A | B | ||
|
||
c; // never | ||
>c : never | ||
} | ||
|
||
if (a in c) { | ||
>a in c : boolean | ||
>a : "a" | ||
>c : A | B | ||
|
||
c; // A | ||
>c : A | ||
|
||
c[a]; // number; | ||
>c[a] : number | ||
>c : A | ||
>a : "a" | ||
} | ||
|
||
if (d in c) { | ||
>d in c : boolean | ||
>d : "d" | ||
>c : A | B | ||
|
||
c; // never | ||
>c : never | ||
} | ||
|
26 changes: 26 additions & 0 deletions
26
tests/cases/conformance/controlFlow/controlFlowInOperator.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,26 @@ | ||
const a = 'a'; | ||
const b = 'b'; | ||
const d = 'd'; | ||
|
||
type A = { [a]: number; }; | ||
type B = { [b]: string; }; | ||
|
||
declare const c: A | B; | ||
|
||
if ('a' in c) { | ||
c; // A | ||
c['a']; // number; | ||
} | ||
|
||
if ('d' in c) { | ||
c; // never | ||
} | ||
|
||
if (a in c) { | ||
c; // A | ||
c[a]; // number; | ||
} | ||
|
||
if (d in c) { | ||
c; // never | ||
} |