Skip to content

Refactor narrow type by instance of #3064

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
May 7, 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
51 changes: 28 additions & 23 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5384,38 +5384,43 @@ module ts {
if (!isTypeSubtypeOf(rightType, globalFunctionType)) {
return type;
}
// Target type is type of prototype property

let targetType: Type;
let prototypeProperty = getPropertyOfType(rightType, "prototype");
if (prototypeProperty) {
let targetType = getTypeOfSymbol(prototypeProperty);
if (targetType !== anyType) {
// Narrow to the target type if it's a subtype of the current type
if (isTypeSubtypeOf(targetType, type)) {
return targetType;
}
// If the current type is a union type, remove all constituents that aren't subtypes of the target.
if (type.flags & TypeFlags.Union) {
return getUnionType(filter((<UnionType>type).types, t => isTypeSubtypeOf(t, targetType)));
}
// Target type is type of the protoype property
let prototypePropertyType = getTypeOfSymbol(prototypeProperty);
if (prototypePropertyType !== anyType) {
targetType = prototypePropertyType;
}
}
// Target type is type of construct signature
let constructSignatures: Signature[];
if (rightType.flags & TypeFlags.Interface) {
constructSignatures = resolveDeclaredMembers(<InterfaceType>rightType).declaredConstructSignatures;
}
else if (rightType.flags & TypeFlags.Anonymous) {
constructSignatures = getSignaturesOfType(rightType, SignatureKind.Construct);

if (!targetType) {
// Target type is type of construct signature
let constructSignatures: Signature[];
if (rightType.flags & TypeFlags.Interface) {
constructSignatures = resolveDeclaredMembers(<InterfaceType>rightType).declaredConstructSignatures;
}
else if (rightType.flags & TypeFlags.Anonymous) {
constructSignatures = getSignaturesOfType(rightType, SignatureKind.Construct);
}

if (constructSignatures && constructSignatures.length) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Put an extra newline here so it looks separate from the prior two conditions

targetType = getUnionType(map(constructSignatures, signature => getReturnTypeOfSignature(getErasedSignature(signature))));
}
}

if (constructSignatures && constructSignatures.length !== 0) {
let instanceType = getUnionType(map(constructSignatures, signature => getReturnTypeOfSignature(getErasedSignature(signature))));
// Pickup type from union types
if (targetType) {
// Narrow to the target type if it's a subtype of the current type
if (isTypeSubtypeOf(targetType, type)) {
return targetType;
}
// If the current type is a union type, remove all constituents that aren't subtypes of the target.
if (type.flags & TypeFlags.Union) {
return getUnionType(filter((<UnionType>type).types, t => isTypeSubtypeOf(t, instanceType)));
return getUnionType(filter((<UnionType>type).types, t => isTypeSubtypeOf(t, targetType)));
}
return instanceType;
}

return type;
}

Expand Down
53 changes: 53 additions & 0 deletions tests/baselines/reference/narrowTypeByInstanceof.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//// [narrowTypeByInstanceof.ts]
class Match {
public range(): any {
return undefined;
}
}

class FileMatch {
public resource(): any {
return undefined;
}
}

type FileMatchOrMatch = FileMatch | Match;


let elementA: FileMatchOrMatch, elementB: FileMatchOrMatch;

if (elementA instanceof FileMatch && elementB instanceof FileMatch) {
let a = elementA.resource().path;
let b = elementB.resource().path;
} else if (elementA instanceof Match && elementB instanceof Match) {
let a = elementA.range();
let b = elementB.range();
}


//// [narrowTypeByInstanceof.js]
var Match = (function () {
function Match() {
}
Match.prototype.range = function () {
return undefined;
};
return Match;
})();
var FileMatch = (function () {
function FileMatch() {
}
FileMatch.prototype.resource = function () {
return undefined;
};
return FileMatch;
})();
var elementA, elementB;
if (elementA instanceof FileMatch && elementB instanceof FileMatch) {
var a = elementA.resource().path;
var b = elementB.resource().path;
}
else if (elementA instanceof Match && elementB instanceof Match) {
var a = elementA.range();
var b = elementB.range();
}
72 changes: 72 additions & 0 deletions tests/baselines/reference/narrowTypeByInstanceof.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
=== tests/cases/compiler/narrowTypeByInstanceof.ts ===
class Match {
>Match : Symbol(Match, Decl(narrowTypeByInstanceof.ts, 0, 0))

public range(): any {
>range : Symbol(range, Decl(narrowTypeByInstanceof.ts, 0, 17))

return undefined;
>undefined : Symbol(undefined)
}
}

class FileMatch {
>FileMatch : Symbol(FileMatch, Decl(narrowTypeByInstanceof.ts, 4, 5))

public resource(): any {
>resource : Symbol(resource, Decl(narrowTypeByInstanceof.ts, 6, 21))

return undefined;
>undefined : Symbol(undefined)
}
}

type FileMatchOrMatch = FileMatch | Match;
>FileMatchOrMatch : Symbol(FileMatchOrMatch, Decl(narrowTypeByInstanceof.ts, 10, 5))
>FileMatch : Symbol(FileMatch, Decl(narrowTypeByInstanceof.ts, 4, 5))
>Match : Symbol(Match, Decl(narrowTypeByInstanceof.ts, 0, 0))


let elementA: FileMatchOrMatch, elementB: FileMatchOrMatch;
>elementA : Symbol(elementA, Decl(narrowTypeByInstanceof.ts, 15, 3))
>FileMatchOrMatch : Symbol(FileMatchOrMatch, Decl(narrowTypeByInstanceof.ts, 10, 5))
>elementB : Symbol(elementB, Decl(narrowTypeByInstanceof.ts, 15, 31))
>FileMatchOrMatch : Symbol(FileMatchOrMatch, Decl(narrowTypeByInstanceof.ts, 10, 5))

if (elementA instanceof FileMatch && elementB instanceof FileMatch) {
>elementA : Symbol(elementA, Decl(narrowTypeByInstanceof.ts, 15, 3))
>FileMatch : Symbol(FileMatch, Decl(narrowTypeByInstanceof.ts, 4, 5))
>elementB : Symbol(elementB, Decl(narrowTypeByInstanceof.ts, 15, 31))
>FileMatch : Symbol(FileMatch, Decl(narrowTypeByInstanceof.ts, 4, 5))

let a = elementA.resource().path;
>a : Symbol(a, Decl(narrowTypeByInstanceof.ts, 18, 7))
>elementA.resource : Symbol(FileMatch.resource, Decl(narrowTypeByInstanceof.ts, 6, 21))
>elementA : Symbol(elementA, Decl(narrowTypeByInstanceof.ts, 15, 3))
>resource : Symbol(FileMatch.resource, Decl(narrowTypeByInstanceof.ts, 6, 21))

let b = elementB.resource().path;
>b : Symbol(b, Decl(narrowTypeByInstanceof.ts, 19, 7))
>elementB.resource : Symbol(FileMatch.resource, Decl(narrowTypeByInstanceof.ts, 6, 21))
>elementB : Symbol(elementB, Decl(narrowTypeByInstanceof.ts, 15, 31))
>resource : Symbol(FileMatch.resource, Decl(narrowTypeByInstanceof.ts, 6, 21))

} else if (elementA instanceof Match && elementB instanceof Match) {
>elementA : Symbol(elementA, Decl(narrowTypeByInstanceof.ts, 15, 3))
>Match : Symbol(Match, Decl(narrowTypeByInstanceof.ts, 0, 0))
>elementB : Symbol(elementB, Decl(narrowTypeByInstanceof.ts, 15, 31))
>Match : Symbol(Match, Decl(narrowTypeByInstanceof.ts, 0, 0))

let a = elementA.range();
>a : Symbol(a, Decl(narrowTypeByInstanceof.ts, 21, 7))
>elementA.range : Symbol(Match.range, Decl(narrowTypeByInstanceof.ts, 0, 17))
>elementA : Symbol(elementA, Decl(narrowTypeByInstanceof.ts, 15, 3))
>range : Symbol(Match.range, Decl(narrowTypeByInstanceof.ts, 0, 17))

let b = elementB.range();
>b : Symbol(b, Decl(narrowTypeByInstanceof.ts, 22, 7))
>elementB.range : Symbol(Match.range, Decl(narrowTypeByInstanceof.ts, 0, 17))
>elementB : Symbol(elementB, Decl(narrowTypeByInstanceof.ts, 15, 31))
>range : Symbol(Match.range, Decl(narrowTypeByInstanceof.ts, 0, 17))
}

86 changes: 86 additions & 0 deletions tests/baselines/reference/narrowTypeByInstanceof.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
=== tests/cases/compiler/narrowTypeByInstanceof.ts ===
class Match {
>Match : Match

public range(): any {
>range : () => any

return undefined;
>undefined : undefined
}
}

class FileMatch {
>FileMatch : FileMatch

public resource(): any {
>resource : () => any

return undefined;
>undefined : undefined
}
}

type FileMatchOrMatch = FileMatch | Match;
>FileMatchOrMatch : Match | FileMatch
>FileMatch : FileMatch
>Match : Match


let elementA: FileMatchOrMatch, elementB: FileMatchOrMatch;
>elementA : Match | FileMatch
>FileMatchOrMatch : Match | FileMatch
>elementB : Match | FileMatch
>FileMatchOrMatch : Match | FileMatch

if (elementA instanceof FileMatch && elementB instanceof FileMatch) {
>elementA instanceof FileMatch && elementB instanceof FileMatch : boolean
>elementA instanceof FileMatch : boolean
>elementA : Match | FileMatch
>FileMatch : typeof FileMatch
>elementB instanceof FileMatch : boolean
>elementB : Match | FileMatch
>FileMatch : typeof FileMatch

let a = elementA.resource().path;
>a : any
>elementA.resource().path : any
>elementA.resource() : any
>elementA.resource : () => any
>elementA : FileMatch
>resource : () => any
>path : any

let b = elementB.resource().path;
>b : any
>elementB.resource().path : any
>elementB.resource() : any
>elementB.resource : () => any
>elementB : FileMatch
>resource : () => any
>path : any

} else if (elementA instanceof Match && elementB instanceof Match) {
>elementA instanceof Match && elementB instanceof Match : boolean
>elementA instanceof Match : boolean
>elementA : Match | FileMatch
>Match : typeof Match
>elementB instanceof Match : boolean
>elementB : Match | FileMatch
>Match : typeof Match

let a = elementA.range();
>a : any
>elementA.range() : any
>elementA.range : () => any
>elementA : Match
>range : () => any

let b = elementB.range();
>b : any
>elementB.range() : any
>elementB.range : () => any
>elementB : Match
>range : () => any
}

Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(12,10): error TS2339: Property 'bar' does not exist on type 'A'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(33,5): error TS2322: Type 'string' is not assignable to type 'number'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(34,10): error TS2339: Property 'bar' does not exist on type 'B<number>'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(63,10): error TS2339: Property 'bar2' does not exist on type 'C1'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(82,10): error TS2339: Property 'bar' does not exist on type 'D'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(109,10): error TS2339: Property 'bar2' does not exist on type 'E1'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(131,11): error TS2339: Property 'foo' does not exist on type 'string | F'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(132,11): error TS2339: Property 'bar' does not exist on type 'string | F'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(157,11): error TS2339: Property 'foo2' does not exist on type 'G1'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(179,11): error TS2339: Property 'bar' does not exist on type 'H'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(65,10): error TS2339: Property 'bar1' does not exist on type 'C1 | C2'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(66,10): error TS2339: Property 'bar2' does not exist on type 'C1 | C2'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(85,10): error TS2339: Property 'bar' does not exist on type 'D'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(111,10): error TS2339: Property 'bar1' does not exist on type 'E1 | E2'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(112,10): error TS2339: Property 'bar2' does not exist on type 'E1 | E2'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(134,11): error TS2339: Property 'foo' does not exist on type 'string | F'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(135,11): error TS2339: Property 'bar' does not exist on type 'string | F'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(160,11): error TS2339: Property 'foo2' does not exist on type 'G1'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(182,11): error TS2339: Property 'bar' does not exist on type 'H'.


==== tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts (10 errors) ====
==== tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts (12 errors) ====
interface AConstructor {
new (): A;
}
Expand Down Expand Up @@ -67,21 +69,26 @@ tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstru
}
interface C1 {
foo: string;
c: string;
bar1: number;
}
interface C2 {
foo: string;
c: string;
bar2: number;
}
declare var C: CConstructor;

var obj5: C1 | A;
if (obj5 instanceof C) { // narrowed to C1.
if (obj5 instanceof C) { // narrowed to C1|C2.
obj5.foo;
obj5.c;
obj5.bar1;
~~~~
!!! error TS2339: Property 'bar1' does not exist on type 'C1 | C2'.
obj5.bar2;
~~~~
!!! error TS2339: Property 'bar2' does not exist on type 'C1'.
!!! error TS2339: Property 'bar2' does not exist on type 'C1 | C2'.
}

var obj6: any;
Expand Down Expand Up @@ -126,12 +133,14 @@ tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstru
declare var E: EConstructor;

var obj9: E1 | A;
if (obj9 instanceof E) { // narrowed to E1.
if (obj9 instanceof E) { // narrowed to E1 | E2
obj9.foo;
obj9.bar1;
~~~~
!!! error TS2339: Property 'bar1' does not exist on type 'E1 | E2'.
obj9.bar2;
~~~~
!!! error TS2339: Property 'bar2' does not exist on type 'E1'.
!!! error TS2339: Property 'bar2' does not exist on type 'E1 | E2'.
}

var obj10: any;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,20 @@ interface CConstructor {
}
interface C1 {
foo: string;
c: string;
bar1: number;
}
interface C2 {
foo: string;
c: string;
bar2: number;
}
declare var C: CConstructor;

var obj5: C1 | A;
if (obj5 instanceof C) { // narrowed to C1.
if (obj5 instanceof C) { // narrowed to C1|C2.
obj5.foo;
obj5.c;
obj5.bar1;
obj5.bar2;
}
Expand Down Expand Up @@ -104,7 +107,7 @@ interface E2 {
declare var E: EConstructor;

var obj9: E1 | A;
if (obj9 instanceof E) { // narrowed to E1.
if (obj9 instanceof E) { // narrowed to E1 | E2
obj9.foo;
obj9.bar1;
obj9.bar2;
Expand Down Expand Up @@ -213,6 +216,7 @@ if (obj4 instanceof B) {
var obj5;
if (obj5 instanceof C) {
obj5.foo;
obj5.c;
obj5.bar1;
obj5.bar2;
}
Expand Down
Loading