From 45ca9d285ffb398c751960f3bd55269403e4f63f Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 12 Nov 2014 12:36:06 -0800 Subject: [PATCH] Add test case for instance of type guard with interface with prototype property --- .../typeGuardOfFormInstanceOfOnInterface.js | 62 +++++++ ...typeGuardOfFormInstanceOfOnInterface.types | 162 ++++++++++++++++++ .../typeGuardOfFormInstanceOfOnInterface.ts | 38 ++++ 3 files changed, 262 insertions(+) create mode 100644 tests/baselines/reference/typeGuardOfFormInstanceOfOnInterface.js create mode 100644 tests/baselines/reference/typeGuardOfFormInstanceOfOnInterface.types create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardOfFormInstanceOfOnInterface.ts diff --git a/tests/baselines/reference/typeGuardOfFormInstanceOfOnInterface.js b/tests/baselines/reference/typeGuardOfFormInstanceOfOnInterface.js new file mode 100644 index 0000000000..2f40be79ac --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormInstanceOfOnInterface.js @@ -0,0 +1,62 @@ +//// [typeGuardOfFormInstanceOfOnInterface.ts] +// A type guard of the form x instanceof C, where C is of a subtype of the global type 'Function' +// and C has a property named 'prototype' +// - when true, narrows the type of x to the type of the 'prototype' property in C provided +// it is a subtype of the type of x, or +// - when false, has no effect on the type of x. + +interface C1 { + (): C1; + prototype: C1; + p1: string; +} +interface C2 { + (): C2; + prototype: C2; + p2: number; +} +interface D1 extends C1 { + prototype: D1; + p3: number; +} +var str: string; +var num: number; +var strOrNum: string | number; + +var c1: C1; +var c2: C2; +var d1: D1; +var c1Orc2: C1 | C2; +str = c1Orc2 instanceof c1 && c1Orc2.p1; // C1 +num = c1Orc2 instanceof c2 && c1Orc2.p2; // C2 +str = c1Orc2 instanceof d1 && c1Orc2.p1; // C1 +num = c1Orc2 instanceof d1 && c1Orc2.p3; // D1 + +var c2Ord1: C2 | D1; +num = c2Ord1 instanceof c2 && c2Ord1.p2; // C2 +num = c2Ord1 instanceof d1 && c2Ord1.p3; // D1 +str = c2Ord1 instanceof d1 && c2Ord1.p1; // D1 +var r2: D1 | C2 = c2Ord1 instanceof c1 && c2Ord1; // C2 | D1 + +//// [typeGuardOfFormInstanceOfOnInterface.js] +// A type guard of the form x instanceof C, where C is of a subtype of the global type 'Function' +// and C has a property named 'prototype' +// - when true, narrows the type of x to the type of the 'prototype' property in C provided +// it is a subtype of the type of x, or +// - when false, has no effect on the type of x. +var str; +var num; +var strOrNum; +var c1; +var c2; +var d1; +var c1Orc2; +str = c1Orc2 instanceof c1 && c1Orc2.p1; // C1 +num = c1Orc2 instanceof c2 && c1Orc2.p2; // C2 +str = c1Orc2 instanceof d1 && c1Orc2.p1; // C1 +num = c1Orc2 instanceof d1 && c1Orc2.p3; // D1 +var c2Ord1; +num = c2Ord1 instanceof c2 && c2Ord1.p2; // C2 +num = c2Ord1 instanceof d1 && c2Ord1.p3; // D1 +str = c2Ord1 instanceof d1 && c2Ord1.p1; // D1 +var r2 = c2Ord1 instanceof c1 && c2Ord1; // C2 | D1 diff --git a/tests/baselines/reference/typeGuardOfFormInstanceOfOnInterface.types b/tests/baselines/reference/typeGuardOfFormInstanceOfOnInterface.types new file mode 100644 index 0000000000..7bf67da150 --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormInstanceOfOnInterface.types @@ -0,0 +1,162 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormInstanceOfOnInterface.ts === +// A type guard of the form x instanceof C, where C is of a subtype of the global type 'Function' +// and C has a property named 'prototype' +// - when true, narrows the type of x to the type of the 'prototype' property in C provided +// it is a subtype of the type of x, or +// - when false, has no effect on the type of x. + +interface C1 { +>C1 : C1 + + (): C1; +>C1 : C1 + + prototype: C1; +>prototype : C1 +>C1 : C1 + + p1: string; +>p1 : string +} +interface C2 { +>C2 : C2 + + (): C2; +>C2 : C2 + + prototype: C2; +>prototype : C2 +>C2 : C2 + + p2: number; +>p2 : number +} +interface D1 extends C1 { +>D1 : D1 +>C1 : C1 + + prototype: D1; +>prototype : D1 +>D1 : D1 + + p3: number; +>p3 : number +} +var str: string; +>str : string + +var num: number; +>num : number + +var strOrNum: string | number; +>strOrNum : string | number + +var c1: C1; +>c1 : C1 +>C1 : C1 + +var c2: C2; +>c2 : C2 +>C2 : C2 + +var d1: D1; +>d1 : D1 +>D1 : D1 + +var c1Orc2: C1 | C2; +>c1Orc2 : C1 | C2 +>C1 : C1 +>C2 : C2 + +str = c1Orc2 instanceof c1 && c1Orc2.p1; // C1 +>str = c1Orc2 instanceof c1 && c1Orc2.p1 : string +>str : string +>c1Orc2 instanceof c1 && c1Orc2.p1 : string +>c1Orc2 instanceof c1 : boolean +>c1Orc2 : C1 | C2 +>c1 : C1 +>c1Orc2.p1 : string +>c1Orc2 : C1 +>p1 : string + +num = c1Orc2 instanceof c2 && c1Orc2.p2; // C2 +>num = c1Orc2 instanceof c2 && c1Orc2.p2 : number +>num : number +>c1Orc2 instanceof c2 && c1Orc2.p2 : number +>c1Orc2 instanceof c2 : boolean +>c1Orc2 : C1 | C2 +>c2 : C2 +>c1Orc2.p2 : number +>c1Orc2 : C2 +>p2 : number + +str = c1Orc2 instanceof d1 && c1Orc2.p1; // C1 +>str = c1Orc2 instanceof d1 && c1Orc2.p1 : string +>str : string +>c1Orc2 instanceof d1 && c1Orc2.p1 : string +>c1Orc2 instanceof d1 : boolean +>c1Orc2 : C1 | C2 +>d1 : D1 +>c1Orc2.p1 : string +>c1Orc2 : D1 +>p1 : string + +num = c1Orc2 instanceof d1 && c1Orc2.p3; // D1 +>num = c1Orc2 instanceof d1 && c1Orc2.p3 : number +>num : number +>c1Orc2 instanceof d1 && c1Orc2.p3 : number +>c1Orc2 instanceof d1 : boolean +>c1Orc2 : C1 | C2 +>d1 : D1 +>c1Orc2.p3 : number +>c1Orc2 : D1 +>p3 : number + +var c2Ord1: C2 | D1; +>c2Ord1 : C2 | D1 +>C2 : C2 +>D1 : D1 + +num = c2Ord1 instanceof c2 && c2Ord1.p2; // C2 +>num = c2Ord1 instanceof c2 && c2Ord1.p2 : number +>num : number +>c2Ord1 instanceof c2 && c2Ord1.p2 : number +>c2Ord1 instanceof c2 : boolean +>c2Ord1 : C2 | D1 +>c2 : C2 +>c2Ord1.p2 : number +>c2Ord1 : C2 +>p2 : number + +num = c2Ord1 instanceof d1 && c2Ord1.p3; // D1 +>num = c2Ord1 instanceof d1 && c2Ord1.p3 : number +>num : number +>c2Ord1 instanceof d1 && c2Ord1.p3 : number +>c2Ord1 instanceof d1 : boolean +>c2Ord1 : C2 | D1 +>d1 : D1 +>c2Ord1.p3 : number +>c2Ord1 : D1 +>p3 : number + +str = c2Ord1 instanceof d1 && c2Ord1.p1; // D1 +>str = c2Ord1 instanceof d1 && c2Ord1.p1 : string +>str : string +>c2Ord1 instanceof d1 && c2Ord1.p1 : string +>c2Ord1 instanceof d1 : boolean +>c2Ord1 : C2 | D1 +>d1 : D1 +>c2Ord1.p1 : string +>c2Ord1 : D1 +>p1 : string + +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 : boolean +>c2Ord1 : C2 | D1 +>c1 : C1 +>c2Ord1 : C2 | D1 + diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormInstanceOfOnInterface.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormInstanceOfOnInterface.ts new file mode 100644 index 0000000000..47e17667bc --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormInstanceOfOnInterface.ts @@ -0,0 +1,38 @@ +// A type guard of the form x instanceof C, where C is of a subtype of the global type 'Function' +// and C has a property named 'prototype' +// - when true, narrows the type of x to the type of the 'prototype' property in C provided +// it is a subtype of the type of x, or +// - when false, has no effect on the type of x. + +interface C1 { + (): C1; + prototype: C1; + p1: string; +} +interface C2 { + (): C2; + prototype: C2; + p2: number; +} +interface D1 extends C1 { + prototype: D1; + p3: number; +} +var str: string; +var num: number; +var strOrNum: string | number; + +var c1: C1; +var c2: C2; +var d1: D1; +var c1Orc2: C1 | C2; +str = c1Orc2 instanceof c1 && c1Orc2.p1; // C1 +num = c1Orc2 instanceof c2 && c1Orc2.p2; // C2 +str = c1Orc2 instanceof d1 && c1Orc2.p1; // C1 +num = c1Orc2 instanceof d1 && c1Orc2.p3; // D1 + +var c2Ord1: C2 | D1; +num = c2Ord1 instanceof c2 && c2Ord1.p2; // C2 +num = c2Ord1 instanceof d1 && c2Ord1.p3; // D1 +str = c2Ord1 instanceof d1 && c2Ord1.p1; // D1 +var r2: D1 | C2 = c2Ord1 instanceof c1 && c2Ord1; // C2 | D1 \ No newline at end of file