Skip to content

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 5 commits into from
Jun 9, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7997,7 +7997,7 @@ namespace ts {
const targetType = type.flags & TypeFlags.TypeParameter ? getApparentType(type) : type;
return isTypeAssignableTo(candidate, targetType) ? candidate :
isTypeAssignableTo(type, candidate) ? type :
neverType;
getIntersectionType([type, candidate]);
}

function narrowTypeByTypePredicate(type: Type, callExpression: CallExpression, assumeTrue: boolean): Type {
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/instanceOfAssignability.types
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ function fn5(x: Derived1) {
// 1.5: y: Derived1
// Want: ???
let y = x;
>y : never
>x : never
>y : Derived1 & Derived2
>x : Derived1 & Derived2
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/stringLiteralTypesAsTags01.types
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,6 @@ if (!hasKind(x, "B")) {
}
else {
let d = x;
>d : never
>x : never
>d : A & B
>x : A & B
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/stringLiteralTypesAsTags02.types
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,6 @@ if (!hasKind(x, "B")) {
}
else {
let d = x;
>d : never
>x : never
>d : A & B
>x : A & B
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/stringLiteralTypesAsTags03.types
Original file line number Diff line number Diff line change
Expand Up @@ -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 tests/baselines/reference/typeGuardIntersectionTypes.js
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
Copy link
Contributor

@yortus yortus Jun 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be #9016


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
}
}
Loading