Skip to content
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

Improved union type guards #1657

Merged
merged 4 commits into from
Jan 13, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 11 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4709,13 +4709,21 @@ module ts {
if (!isTypeSubtypeOf(rightType, globalFunctionType)) {
return type;
}
// Target type is type of prototype property
var prototypeProperty = getPropertyOfType(rightType, "prototype");
if (!prototypeProperty) {
return type;
}
var prototypeType = getTypeOfSymbol(prototypeProperty);
// Narrow to type of prototype property if it is a subtype of current type
return isTypeSubtypeOf(prototypeType, type) ? prototypeType : type;
var targetType = getTypeOfSymbol(prototypeProperty);
// Narrow to target type if it is a subtype of current type
if (isTypeSubtypeOf(targetType, type)) {
return targetType;
}
// If current type is a union type, remove all constituents that aren't subtypes of target type
if (type.flags && TypeFlags.Union) {
return getUnionType(filter((<UnionType>type).types, t => isTypeSubtypeOf(t, targetType)));
}
return type;
}

// Narrow the given type based on the given expression having the assumed boolean value
Expand Down
23 changes: 23 additions & 0 deletions tests/baselines/reference/TypeGuardWithArrayUnion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [TypeGuardWithArrayUnion.ts]
class Message {
value: string;
}

function saySize(message: Message | Message[]) {
if (message instanceof Array) {
return message.length; // Should have type Message[] here
}
}


//// [TypeGuardWithArrayUnion.js]
var Message = (function () {
function Message() {
}
return Message;
})();
function saySize(message) {
if (message instanceof Array) {
return message.length; // Should have type Message[] here
}
}
26 changes: 26 additions & 0 deletions tests/baselines/reference/TypeGuardWithArrayUnion.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
=== tests/cases/conformance/expressions/typeGuards/TypeGuardWithArrayUnion.ts ===
class Message {
>Message : Message

value: string;
>value : string
}

function saySize(message: Message | Message[]) {
>saySize : (message: Message | Message[]) => number
>message : Message | Message[]
>Message : Message
>Message : Message

if (message instanceof Array) {
>message instanceof Array : boolean
>message : Message | Message[]
>Array : ArrayConstructor

return message.length; // Should have type Message[] here
>message.length : number
>message : Message[]
>length : number
}
}

4 changes: 2 additions & 2 deletions tests/baselines/reference/typeGuardOfFormInstanceOf.types
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ var r2: D1 | C2 = c2Ord1 instanceof C1 && c2Ord1; // C2 | D1
>r2 : C2 | D1
>D1 : D1
>C2 : C2
>c2Ord1 instanceof C1 && c2Ord1 : C2 | D1
>c2Ord1 instanceof C1 && c2Ord1 : D1
>c2Ord1 instanceof C1 : boolean
>c2Ord1 : C2 | D1
>C1 : typeof C1
>c2Ord1 : C2 | D1
>c2Ord1 : D1

Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ var r2: D1 | C2 = c2Ord1 instanceof c1 && c2Ord1; // C2 | D1
>r2 : C2 | D1
>D1 : D1
>C2 : C2
>c2Ord1 instanceof c1 && c2Ord1 : C2 | D1
>c2Ord1 instanceof c1 && c2Ord1 : D1
>c2Ord1 instanceof c1 : boolean
>c2Ord1 : C2 | D1
>c1 : C1
>c2Ord1 : C2 | D1
>c2Ord1 : D1

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Message {
value: string;
}

function saySize(message: Message | Message[]) {
if (message instanceof Array) {
return message.length; // Should have type Message[] here
}
}