-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type guard intersection types #9031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -116,6 +116,6 @@ if (!hasKind(x, "B")) { | |
} | ||
else { | ||
let d = x; | ||
>d : never | ||
>x : never | ||
>d : A & B | ||
>x : A & B | ||
} |
This file contains hidden or 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 |
---|---|---|
|
@@ -110,6 +110,6 @@ if (!hasKind(x, "B")) { | |
} | ||
else { | ||
let d = x; | ||
>d : never | ||
>x : never | ||
>d : A & B | ||
>x : A & B | ||
} |
This file contains hidden or 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 |
---|---|---|
|
@@ -113,6 +113,6 @@ if (!hasKind(x, "B")) { | |
} | ||
else { | ||
let d = x; | ||
>d : never | ||
>x : never | ||
>d : A & B | ||
>x : A & B | ||
} |
181 changes: 181 additions & 0 deletions
181
tests/baselines/reference/typeGuardIntersectionTypes.js
This file contains hidden or 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,181 @@ | ||
//// [typeGuardIntersectionTypes.ts] | ||
|
||
interface X { | ||
x: string; | ||
} | ||
|
||
interface Y { | ||
y: string; | ||
} | ||
|
||
interface Z { | ||
z: string; | ||
} | ||
|
||
declare function isX(obj: any): obj is X; | ||
declare function isY(obj: any): obj is Y; | ||
declare function isZ(obj: any): obj is Z; | ||
|
||
function f1(obj: Object) { | ||
if (isX(obj) || isY(obj) || isZ(obj)) { | ||
obj; | ||
} | ||
if (isX(obj) && isY(obj) && isZ(obj)) { | ||
obj; | ||
} | ||
} | ||
|
||
// Repro from #8911 | ||
|
||
// two interfaces | ||
interface A { | ||
a: string; | ||
} | ||
|
||
interface B { | ||
b: string; | ||
} | ||
|
||
// a type guard for B | ||
function isB(toTest: any): toTest is B { | ||
return toTest && toTest.b; | ||
} | ||
|
||
// a function that turns an A into an A & B | ||
function union(a: A): A & B | null { | ||
if (isB(a)) { | ||
return a; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
// Repro from #9011 | ||
|
||
declare function log(s: string): void; | ||
|
||
// Supported beast features | ||
interface Beast { wings?: boolean; legs?: number } | ||
interface Legged { legs: number; } | ||
interface Winged { wings: boolean; } | ||
|
||
// Beast feature detection via user-defined type guards | ||
function hasLegs(x: Beast): x is Legged { return x && typeof x.legs === 'number'; } | ||
function hasWings(x: Beast): x is Winged { return x && !!x.wings; } | ||
|
||
// Function to identify a given beast by detecting its features | ||
function identifyBeast(beast: Beast) { | ||
|
||
// All beasts with legs | ||
if (hasLegs(beast)) { | ||
|
||
// All winged beasts with legs | ||
if (hasWings(beast)) { | ||
if (beast.legs === 4) { // ERROR TS2339: Property 'legs' does not exist on type 'Winged'. | ||
log(`pegasus - 4 legs, wings`); | ||
} | ||
else if (beast.legs === 2) { // ERROR TS2339... | ||
log(`bird - 2 legs, wings`); | ||
} | ||
else { | ||
log(`unknown - ${beast.legs} legs, wings`); // ERROR TS2339... | ||
} | ||
} | ||
|
||
// All non-winged beasts with legs | ||
else { | ||
log(`manbearpig - ${beast.legs} legs, no wings`); | ||
} | ||
} | ||
|
||
// All beasts without legs | ||
else { | ||
if (hasWings(beast)) { | ||
log(`quetzalcoatl - no legs, wings`) | ||
} | ||
else { | ||
log(`snake - no legs, no wings`) | ||
} | ||
} | ||
} | ||
|
||
function beastFoo(beast: Object) { | ||
if (hasWings(beast) && hasLegs(beast)) { | ||
beast // beast is Legged | ||
// ideally, beast would be Winged && Legged here... | ||
} | ||
else { | ||
beast | ||
} | ||
|
||
if (hasLegs(beast) && hasWings(beast)) { | ||
beast // beast is Winged | ||
// ideally, beast would be Legged && Winged here... | ||
} | ||
} | ||
|
||
//// [typeGuardIntersectionTypes.js] | ||
function f1(obj) { | ||
if (isX(obj) || isY(obj) || isZ(obj)) { | ||
obj; | ||
} | ||
if (isX(obj) && isY(obj) && isZ(obj)) { | ||
obj; | ||
} | ||
} | ||
// a type guard for B | ||
function isB(toTest) { | ||
return toTest && toTest.b; | ||
} | ||
// a function that turns an A into an A & B | ||
function union(a) { | ||
if (isB(a)) { | ||
return a; | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
// Beast feature detection via user-defined type guards | ||
function hasLegs(x) { return x && typeof x.legs === 'number'; } | ||
function hasWings(x) { return x && !!x.wings; } | ||
// Function to identify a given beast by detecting its features | ||
function identifyBeast(beast) { | ||
// All beasts with legs | ||
if (hasLegs(beast)) { | ||
// All winged beasts with legs | ||
if (hasWings(beast)) { | ||
if (beast.legs === 4) { | ||
log("pegasus - 4 legs, wings"); | ||
} | ||
else if (beast.legs === 2) { | ||
log("bird - 2 legs, wings"); | ||
} | ||
else { | ||
log("unknown - " + beast.legs + " legs, wings"); // ERROR TS2339... | ||
} | ||
} | ||
else { | ||
log("manbearpig - " + beast.legs + " legs, no wings"); | ||
} | ||
} | ||
else { | ||
if (hasWings(beast)) { | ||
log("quetzalcoatl - no legs, wings"); | ||
} | ||
else { | ||
log("snake - no legs, no wings"); | ||
} | ||
} | ||
} | ||
function beastFoo(beast) { | ||
if (hasWings(beast) && hasLegs(beast)) { | ||
beast; // beast is Legged | ||
} | ||
else { | ||
beast; | ||
} | ||
if (hasLegs(beast) && hasWings(beast)) { | ||
beast; // beast is Winged | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be #9016