-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Narrow by comparisons to boolean literals
- Loading branch information
Showing
5 changed files
with
277 additions
and
1 deletion.
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
98 changes: 98 additions & 0 deletions
98
tests/baselines/reference/narrowByBooleanComparison.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,98 @@ | ||
=== tests/cases/compiler/narrowByBooleanComparison.ts === | ||
type A = { type: "A" }; | ||
>A : Symbol(A, Decl(narrowByBooleanComparison.ts, 0, 0)) | ||
>type : Symbol(type, Decl(narrowByBooleanComparison.ts, 0, 10)) | ||
|
||
type B = { type: "B" }; | ||
>B : Symbol(B, Decl(narrowByBooleanComparison.ts, 0, 23)) | ||
>type : Symbol(type, Decl(narrowByBooleanComparison.ts, 1, 10)) | ||
|
||
type C = { type: "C" }; | ||
>C : Symbol(C, Decl(narrowByBooleanComparison.ts, 1, 23)) | ||
>type : Symbol(type, Decl(narrowByBooleanComparison.ts, 2, 10)) | ||
|
||
type MyUnion = A | B | C; | ||
>MyUnion : Symbol(MyUnion, Decl(narrowByBooleanComparison.ts, 2, 23)) | ||
>A : Symbol(A, Decl(narrowByBooleanComparison.ts, 0, 0)) | ||
>B : Symbol(B, Decl(narrowByBooleanComparison.ts, 0, 23)) | ||
>C : Symbol(C, Decl(narrowByBooleanComparison.ts, 1, 23)) | ||
|
||
const isA = (x: MyUnion): x is A => x.type === "A"; | ||
>isA : Symbol(isA, Decl(narrowByBooleanComparison.ts, 5, 5)) | ||
>x : Symbol(x, Decl(narrowByBooleanComparison.ts, 5, 13)) | ||
>MyUnion : Symbol(MyUnion, Decl(narrowByBooleanComparison.ts, 2, 23)) | ||
>x : Symbol(x, Decl(narrowByBooleanComparison.ts, 5, 13)) | ||
>A : Symbol(A, Decl(narrowByBooleanComparison.ts, 0, 0)) | ||
>x.type : Symbol(type, Decl(narrowByBooleanComparison.ts, 0, 10), Decl(narrowByBooleanComparison.ts, 1, 10), Decl(narrowByBooleanComparison.ts, 2, 10)) | ||
>x : Symbol(x, Decl(narrowByBooleanComparison.ts, 5, 13)) | ||
>type : Symbol(type, Decl(narrowByBooleanComparison.ts, 0, 10), Decl(narrowByBooleanComparison.ts, 1, 10), Decl(narrowByBooleanComparison.ts, 2, 10)) | ||
|
||
function test1(x: MyUnion) { | ||
>test1 : Symbol(test1, Decl(narrowByBooleanComparison.ts, 5, 51)) | ||
>x : Symbol(x, Decl(narrowByBooleanComparison.ts, 7, 15)) | ||
>MyUnion : Symbol(MyUnion, Decl(narrowByBooleanComparison.ts, 2, 23)) | ||
|
||
if (isA(x) !== true) { | ||
>isA : Symbol(isA, Decl(narrowByBooleanComparison.ts, 5, 5)) | ||
>x : Symbol(x, Decl(narrowByBooleanComparison.ts, 7, 15)) | ||
|
||
x | ||
>x : Symbol(x, Decl(narrowByBooleanComparison.ts, 7, 15)) | ||
} | ||
|
||
if (isA(x) !== false) { | ||
>isA : Symbol(isA, Decl(narrowByBooleanComparison.ts, 5, 5)) | ||
>x : Symbol(x, Decl(narrowByBooleanComparison.ts, 7, 15)) | ||
|
||
x | ||
>x : Symbol(x, Decl(narrowByBooleanComparison.ts, 7, 15)) | ||
} | ||
|
||
if (isA(x) === false) { | ||
>isA : Symbol(isA, Decl(narrowByBooleanComparison.ts, 5, 5)) | ||
>x : Symbol(x, Decl(narrowByBooleanComparison.ts, 7, 15)) | ||
|
||
x | ||
>x : Symbol(x, Decl(narrowByBooleanComparison.ts, 7, 15)) | ||
} | ||
|
||
if (isA(x) === true) { | ||
>isA : Symbol(isA, Decl(narrowByBooleanComparison.ts, 5, 5)) | ||
>x : Symbol(x, Decl(narrowByBooleanComparison.ts, 7, 15)) | ||
|
||
x | ||
>x : Symbol(x, Decl(narrowByBooleanComparison.ts, 7, 15)) | ||
} | ||
|
||
if (isA(x) != true) { | ||
>isA : Symbol(isA, Decl(narrowByBooleanComparison.ts, 5, 5)) | ||
>x : Symbol(x, Decl(narrowByBooleanComparison.ts, 7, 15)) | ||
|
||
x | ||
>x : Symbol(x, Decl(narrowByBooleanComparison.ts, 7, 15)) | ||
} | ||
|
||
if (isA(x) == true) { | ||
>isA : Symbol(isA, Decl(narrowByBooleanComparison.ts, 5, 5)) | ||
>x : Symbol(x, Decl(narrowByBooleanComparison.ts, 7, 15)) | ||
|
||
x | ||
>x : Symbol(x, Decl(narrowByBooleanComparison.ts, 7, 15)) | ||
} | ||
|
||
if (true !== isA(x)) { | ||
>isA : Symbol(isA, Decl(narrowByBooleanComparison.ts, 5, 5)) | ||
>x : Symbol(x, Decl(narrowByBooleanComparison.ts, 7, 15)) | ||
|
||
x | ||
>x : Symbol(x, Decl(narrowByBooleanComparison.ts, 7, 15)) | ||
} | ||
|
||
if (true === isA(x)) { | ||
>isA : Symbol(isA, Decl(narrowByBooleanComparison.ts, 5, 5)) | ||
>x : Symbol(x, Decl(narrowByBooleanComparison.ts, 7, 15)) | ||
|
||
x | ||
>x : Symbol(x, Decl(narrowByBooleanComparison.ts, 7, 15)) | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
tests/baselines/reference/narrowByBooleanComparison.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,118 @@ | ||
=== tests/cases/compiler/narrowByBooleanComparison.ts === | ||
type A = { type: "A" }; | ||
>A : { type: "A"; } | ||
>type : "A" | ||
|
||
type B = { type: "B" }; | ||
>B : { type: "B"; } | ||
>type : "B" | ||
|
||
type C = { type: "C" }; | ||
>C : { type: "C"; } | ||
>type : "C" | ||
|
||
type MyUnion = A | B | C; | ||
>MyUnion : A | B | C | ||
|
||
const isA = (x: MyUnion): x is A => x.type === "A"; | ||
>isA : (x: MyUnion) => x is A | ||
>(x: MyUnion): x is A => x.type === "A" : (x: MyUnion) => x is A | ||
>x : MyUnion | ||
>x.type === "A" : boolean | ||
>x.type : "A" | "B" | "C" | ||
>x : MyUnion | ||
>type : "A" | "B" | "C" | ||
>"A" : "A" | ||
|
||
function test1(x: MyUnion) { | ||
>test1 : (x: MyUnion) => void | ||
>x : MyUnion | ||
|
||
if (isA(x) !== true) { | ||
>isA(x) !== true : boolean | ||
>isA(x) : boolean | ||
>isA : (x: MyUnion) => x is A | ||
>x : MyUnion | ||
>true : true | ||
|
||
x | ||
>x : B | C | ||
} | ||
|
||
if (isA(x) !== false) { | ||
>isA(x) !== false : boolean | ||
>isA(x) : boolean | ||
>isA : (x: MyUnion) => x is A | ||
>x : MyUnion | ||
>false : false | ||
|
||
x | ||
>x : A | ||
} | ||
|
||
if (isA(x) === false) { | ||
>isA(x) === false : boolean | ||
>isA(x) : boolean | ||
>isA : (x: MyUnion) => x is A | ||
>x : MyUnion | ||
>false : false | ||
|
||
x | ||
>x : B | C | ||
} | ||
|
||
if (isA(x) === true) { | ||
>isA(x) === true : boolean | ||
>isA(x) : boolean | ||
>isA : (x: MyUnion) => x is A | ||
>x : MyUnion | ||
>true : true | ||
|
||
x | ||
>x : A | ||
} | ||
|
||
if (isA(x) != true) { | ||
>isA(x) != true : boolean | ||
>isA(x) : boolean | ||
>isA : (x: MyUnion) => x is A | ||
>x : MyUnion | ||
>true : true | ||
|
||
x | ||
>x : B | C | ||
} | ||
|
||
if (isA(x) == true) { | ||
>isA(x) == true : boolean | ||
>isA(x) : boolean | ||
>isA : (x: MyUnion) => x is A | ||
>x : MyUnion | ||
>true : true | ||
|
||
x | ||
>x : A | ||
} | ||
|
||
if (true !== isA(x)) { | ||
>true !== isA(x) : boolean | ||
>true : true | ||
>isA(x) : boolean | ||
>isA : (x: MyUnion) => x is A | ||
>x : MyUnion | ||
|
||
x | ||
>x : B | C | ||
} | ||
|
||
if (true === isA(x)) { | ||
>true === isA(x) : boolean | ||
>true : true | ||
>isA(x) : boolean | ||
>isA : (x: MyUnion) => x is A | ||
>x : MyUnion | ||
|
||
x | ||
>x : A | ||
} | ||
} |
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 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
|
||
type A = { type: "A" }; | ||
type B = { type: "B" }; | ||
type C = { type: "C" }; | ||
type MyUnion = A | B | C; | ||
|
||
const isA = (x: MyUnion): x is A => x.type === "A"; | ||
|
||
function test1(x: MyUnion) { | ||
if (isA(x) !== true) { | ||
x | ||
} | ||
|
||
if (isA(x) !== false) { | ||
x | ||
} | ||
|
||
if (isA(x) === false) { | ||
x | ||
} | ||
|
||
if (isA(x) === true) { | ||
x | ||
} | ||
|
||
if (isA(x) != true) { | ||
x | ||
} | ||
|
||
if (isA(x) == true) { | ||
x | ||
} | ||
|
||
if (true !== isA(x)) { | ||
x | ||
} | ||
|
||
if (true === isA(x)) { | ||
x | ||
} | ||
} |