Merge branch 'release-1.5' of https://github.com/Microsoft/TypeScript into removeSubtypesRecursion

This commit is contained in:
Jason Freeman 2015-05-07 15:08:29 -07:00
commit 7a92282af3
13 changed files with 534 additions and 41 deletions

View file

@ -5398,38 +5398,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 (constructSignatures && constructSignatures.length !== 0) {
let instanceType = getUnionType(map(constructSignatures, signature => getReturnTypeOfSignature(getErasedSignature(signature))));
// Pickup type from union types
if (type.flags & TypeFlags.Union) {
return getUnionType(filter((<UnionType>type).types, t => isTypeSubtypeOf(t, instanceType)));
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) {
targetType = getUnionType(map(constructSignatures, signature => getReturnTypeOfSignature(getErasedSignature(signature))));
}
return instanceType;
}
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, targetType)));
}
}
return type;
}

2
src/lib/es6.d.ts vendored
View file

@ -3580,6 +3580,7 @@ interface PromiseLike<T> {
* @returns A Promise for the completion of which ever callback is executed.
*/
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => TResult | PromiseLike<TResult>): PromiseLike<TResult>;
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => void): PromiseLike<TResult>;
}
/**
@ -3593,6 +3594,7 @@ interface Promise<T> {
* @returns A Promise for the completion of which ever callback is executed.
*/
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => TResult | PromiseLike<TResult>): Promise<TResult>;
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => void): Promise<TResult>;
/**
* Attaches a callback for only the rejection of the Promise.

View file

@ -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();
}

View file

@ -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))
}

View file

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

View file

@ -0,0 +1,43 @@
//// [promiseVoidErrorCallback.ts]
interface T1 {
__t1: string;
}
interface T2 {
__t2: string;
}
interface T3 {
__t3: string;
}
function f1(): Promise<T1> {
return Promise.resolve({ __t1: "foo_t1" });
}
function f2(x: T1): T2 {
return { __t2: x.__t1 + ":foo_21" };
}
var x3 = f1()
.then(f2, (e: Error) => {
throw e;
})
.then((x: T2) => {
return { __t3: x.__t2 + "bar" };
});
//// [promiseVoidErrorCallback.js]
function f1() {
return Promise.resolve({ __t1: "foo_t1" });
}
function f2(x) {
return { __t2: x.__t1 + ":foo_21" };
}
var x3 = f1()
.then(f2, (e) => {
throw e;
})
.then((x) => {
return { __t3: x.__t2 + "bar" };
});

View file

@ -0,0 +1,75 @@
=== tests/cases/compiler/promiseVoidErrorCallback.ts ===
interface T1 {
>T1 : Symbol(T1, Decl(promiseVoidErrorCallback.ts, 0, 0))
__t1: string;
>__t1 : Symbol(__t1, Decl(promiseVoidErrorCallback.ts, 0, 14))
}
interface T2 {
>T2 : Symbol(T2, Decl(promiseVoidErrorCallback.ts, 2, 1))
__t2: string;
>__t2 : Symbol(__t2, Decl(promiseVoidErrorCallback.ts, 4, 14))
}
interface T3 {
>T3 : Symbol(T3, Decl(promiseVoidErrorCallback.ts, 6, 1))
__t3: string;
>__t3 : Symbol(__t3, Decl(promiseVoidErrorCallback.ts, 8, 14))
}
function f1(): Promise<T1> {
>f1 : Symbol(f1, Decl(promiseVoidErrorCallback.ts, 10, 1))
>Promise : Symbol(Promise, Decl(lib.d.ts, 4769, 1), Decl(lib.d.ts, 4854, 11))
>T1 : Symbol(T1, Decl(promiseVoidErrorCallback.ts, 0, 0))
return Promise.resolve({ __t1: "foo_t1" });
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.d.ts, 4836, 39), Decl(lib.d.ts, 4843, 54))
>Promise : Symbol(Promise, Decl(lib.d.ts, 4769, 1), Decl(lib.d.ts, 4854, 11))
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.d.ts, 4836, 39), Decl(lib.d.ts, 4843, 54))
>__t1 : Symbol(__t1, Decl(promiseVoidErrorCallback.ts, 13, 28))
}
function f2(x: T1): T2 {
>f2 : Symbol(f2, Decl(promiseVoidErrorCallback.ts, 14, 1))
>x : Symbol(x, Decl(promiseVoidErrorCallback.ts, 16, 12))
>T1 : Symbol(T1, Decl(promiseVoidErrorCallback.ts, 0, 0))
>T2 : Symbol(T2, Decl(promiseVoidErrorCallback.ts, 2, 1))
return { __t2: x.__t1 + ":foo_21" };
>__t2 : Symbol(__t2, Decl(promiseVoidErrorCallback.ts, 17, 12))
>x.__t1 : Symbol(T1.__t1, Decl(promiseVoidErrorCallback.ts, 0, 14))
>x : Symbol(x, Decl(promiseVoidErrorCallback.ts, 16, 12))
>__t1 : Symbol(T1.__t1, Decl(promiseVoidErrorCallback.ts, 0, 14))
}
var x3 = f1()
>x3 : Symbol(x3, Decl(promiseVoidErrorCallback.ts, 20, 3))
>f1() .then(f2, (e: Error) => { throw e;}) .then : Symbol(Promise.then, Decl(lib.d.ts, 4774, 22), Decl(lib.d.ts, 4781, 158))
>f1() .then : Symbol(Promise.then, Decl(lib.d.ts, 4774, 22), Decl(lib.d.ts, 4781, 158))
>f1 : Symbol(f1, Decl(promiseVoidErrorCallback.ts, 10, 1))
.then(f2, (e: Error) => {
>then : Symbol(Promise.then, Decl(lib.d.ts, 4774, 22), Decl(lib.d.ts, 4781, 158))
>f2 : Symbol(f2, Decl(promiseVoidErrorCallback.ts, 14, 1))
>e : Symbol(e, Decl(promiseVoidErrorCallback.ts, 21, 15))
>Error : Symbol(Error, Decl(lib.d.ts, 876, 38), Decl(lib.d.ts, 889, 11))
throw e;
>e : Symbol(e, Decl(promiseVoidErrorCallback.ts, 21, 15))
})
.then((x: T2) => {
>then : Symbol(Promise.then, Decl(lib.d.ts, 4774, 22), Decl(lib.d.ts, 4781, 158))
>x : Symbol(x, Decl(promiseVoidErrorCallback.ts, 24, 11))
>T2 : Symbol(T2, Decl(promiseVoidErrorCallback.ts, 2, 1))
return { __t3: x.__t2 + "bar" };
>__t3 : Symbol(__t3, Decl(promiseVoidErrorCallback.ts, 25, 12))
>x.__t2 : Symbol(T2.__t2, Decl(promiseVoidErrorCallback.ts, 4, 14))
>x : Symbol(x, Decl(promiseVoidErrorCallback.ts, 24, 11))
>__t2 : Symbol(T2.__t2, Decl(promiseVoidErrorCallback.ts, 4, 14))
});

View file

@ -0,0 +1,89 @@
=== tests/cases/compiler/promiseVoidErrorCallback.ts ===
interface T1 {
>T1 : T1
__t1: string;
>__t1 : string
}
interface T2 {
>T2 : T2
__t2: string;
>__t2 : string
}
interface T3 {
>T3 : T3
__t3: string;
>__t3 : string
}
function f1(): Promise<T1> {
>f1 : () => Promise<T1>
>Promise : Promise<T>
>T1 : T1
return Promise.resolve({ __t1: "foo_t1" });
>Promise.resolve({ __t1: "foo_t1" }) : Promise<{ __t1: string; }>
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>Promise : PromiseConstructor
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
>{ __t1: "foo_t1" } : { __t1: string; }
>__t1 : string
>"foo_t1" : string
}
function f2(x: T1): T2 {
>f2 : (x: T1) => T2
>x : T1
>T1 : T1
>T2 : T2
return { __t2: x.__t1 + ":foo_21" };
>{ __t2: x.__t1 + ":foo_21" } : { __t2: string; }
>__t2 : string
>x.__t1 + ":foo_21" : string
>x.__t1 : string
>x : T1
>__t1 : string
>":foo_21" : string
}
var x3 = f1()
>x3 : Promise<{ __t3: string; }>
>f1() .then(f2, (e: Error) => { throw e;}) .then((x: T2) => { return { __t3: x.__t2 + "bar" };}) : Promise<{ __t3: string; }>
>f1() .then(f2, (e: Error) => { throw e;}) .then : { <TResult>(onfulfilled?: (value: T2) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => TResult | PromiseLike<TResult>): Promise<TResult>; <TResult>(onfulfilled?: (value: T2) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => void): Promise<TResult>; }
>f1() .then(f2, (e: Error) => { throw e;}) : Promise<T2>
>f1() .then : { <TResult>(onfulfilled?: (value: T1) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => TResult | PromiseLike<TResult>): Promise<TResult>; <TResult>(onfulfilled?: (value: T1) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => void): Promise<TResult>; }
>f1() : Promise<T1>
>f1 : () => Promise<T1>
.then(f2, (e: Error) => {
>then : { <TResult>(onfulfilled?: (value: T1) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => TResult | PromiseLike<TResult>): Promise<TResult>; <TResult>(onfulfilled?: (value: T1) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => void): Promise<TResult>; }
>f2 : (x: T1) => T2
>(e: Error) => { throw e;} : (e: Error) => void
>e : Error
>Error : Error
throw e;
>e : Error
})
.then((x: T2) => {
>then : { <TResult>(onfulfilled?: (value: T2) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => TResult | PromiseLike<TResult>): Promise<TResult>; <TResult>(onfulfilled?: (value: T2) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => void): Promise<TResult>; }
>(x: T2) => { return { __t3: x.__t2 + "bar" };} : (x: T2) => { __t3: string; }
>x : T2
>T2 : T2
return { __t3: x.__t2 + "bar" };
>{ __t3: x.__t2 + "bar" } : { __t3: string; }
>__t3 : string
>x.__t2 + "bar" : string
>x.__t2 : string
>x : T2
>__t2 : string
>"bar" : string
});

View file

@ -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;
}
@ -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;
@ -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;

View file

@ -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;
}
@ -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;
@ -213,6 +216,7 @@ if (obj4 instanceof B) {
var obj5;
if (obj5 instanceof C) {
obj5.foo;
obj5.c;
obj5.bar1;
obj5.bar2;
}

View file

@ -0,0 +1,24 @@
 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();
}

View file

@ -0,0 +1,28 @@
//@target: ES6
interface T1 {
__t1: string;
}
interface T2 {
__t2: string;
}
interface T3 {
__t3: string;
}
function f1(): Promise<T1> {
return Promise.resolve({ __t1: "foo_t1" });
}
function f2(x: T1): T2 {
return { __t2: x.__t1 + ":foo_21" };
}
var x3 = f1()
.then(f2, (e: Error) => {
throw e;
})
.then((x: T2) => {
return { __t3: x.__t2 + "bar" };
});

View file

@ -48,17 +48,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;
}
@ -103,7 +106,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;