From 5961ed7154f692694c793ffececc6ee7b6696565 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 5 Nov 2014 18:50:43 -0800 Subject: [PATCH] =?UTF-8?q?Test=20typeguard=20of=20form=20instance=20of=20?= =?UTF-8?q?=E2=80=A2=09A=20type=20guard=20of=20the=20form=20x=20instanceof?= =?UTF-8?q?=20C,=20where=20C=20is=20of=20a=20subtype=20of=20the=20global?= =?UTF-8?q?=20type=20=E2=80=98Function=E2=80=99=20and=20C=20has=20a=20prop?= =?UTF-8?q?erty=20named=20=E2=80=98prototype=E2=80=99=20o=09when=20true,?= =?UTF-8?q?=20narrows=20the=20type=20of=20x=20to=20the=20type=20of=20the?= =?UTF-8?q?=20=E2=80=98prototype=E2=80=99=20property=20in=20C=20provided?= =?UTF-8?q?=20it=20is=20a=20subtype=20of=20the=20type=20of=20x,=20or=20o?= =?UTF-8?q?=09when=20false,=20has=20no=20effect=20on=20the=20type=20of=20x?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../reference/typeGuardOfFormInstanceOf.js | 74 ++++++++++ .../reference/typeGuardOfFormInstanceOf.types | 132 ++++++++++++++++++ .../typeGuards/typeGuardOfFormInstanceOf.ts | 30 ++++ 3 files changed, 236 insertions(+) create mode 100644 tests/baselines/reference/typeGuardOfFormInstanceOf.js create mode 100644 tests/baselines/reference/typeGuardOfFormInstanceOf.types create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardOfFormInstanceOf.ts diff --git a/tests/baselines/reference/typeGuardOfFormInstanceOf.js b/tests/baselines/reference/typeGuardOfFormInstanceOf.js new file mode 100644 index 0000000000..093e80cbb7 --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormInstanceOf.js @@ -0,0 +1,74 @@ +//// [typeGuardOfFormInstanceOf.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. + +class C1 { + p1: string; +} +class C2 { + p2: number; +} +class D1 extends C1 { + p3: number; +} +var str: string; +var num: number; +var strOrNum: string | number; + +var c1Orc2: C1 | C2; +str = c1Orc2 instanceof C1 && c1Orc2.p1; // C1 +num = c1Orc2 instanceof C2 && c1Orc2.p2; // C2 +str = c1Orc2 instanceof D1 && c1Orc2.p1; // D1 +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 + +//// [typeGuardOfFormInstanceOf.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 __extends = this.__extends || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + __.prototype = b.prototype; + d.prototype = new __(); +}; +var C1 = (function () { + function C1() { + } + return C1; +})(); +var C2 = (function () { + function C2() { + } + return C2; +})(); +var D1 = (function (_super) { + __extends(D1, _super); + function D1() { + _super.apply(this, arguments); + } + return D1; +})(C1); +var str; +var num; +var strOrNum; +var c1Orc2; +str = c1Orc2 instanceof C1 && c1Orc2.p1; // C1 +num = c1Orc2 instanceof C2 && c1Orc2.p2; // C2 +str = c1Orc2 instanceof D1 && c1Orc2.p1; // D1 +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/typeGuardOfFormInstanceOf.types b/tests/baselines/reference/typeGuardOfFormInstanceOf.types new file mode 100644 index 0000000000..fb44482c86 --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormInstanceOf.types @@ -0,0 +1,132 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormInstanceOf.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. + +class C1 { +>C1 : C1 + + p1: string; +>p1 : string +} +class C2 { +>C2 : C2 + + p2: number; +>p2 : number +} +class D1 extends C1 { +>D1 : D1 +>C1 : C1 + + p3: number; +>p3 : number +} +var str: string; +>str : string + +var num: number; +>num : number + +var strOrNum: string | number; +>strOrNum : string | number + +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 : typeof 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 : typeof C2 +>c1Orc2.p2 : number +>c1Orc2 : C2 +>p2 : number + +str = c1Orc2 instanceof D1 && c1Orc2.p1; // D1 +>str = c1Orc2 instanceof D1 && c1Orc2.p1 : string +>str : string +>c1Orc2 instanceof D1 && c1Orc2.p1 : string +>c1Orc2 instanceof D1 : boolean +>c1Orc2 : C1 | C2 +>D1 : typeof 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 : typeof 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 : typeof 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 : typeof 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 : typeof 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 : typeof C1 +>c2Ord1 : C2 | D1 + diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormInstanceOf.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormInstanceOf.ts new file mode 100644 index 0000000000..233909f44a --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormInstanceOf.ts @@ -0,0 +1,30 @@ +// 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. + +class C1 { + p1: string; +} +class C2 { + p2: number; +} +class D1 extends C1 { + p3: number; +} +var str: string; +var num: number; +var strOrNum: string | number; + +var c1Orc2: C1 | C2; +str = c1Orc2 instanceof C1 && c1Orc2.p1; // C1 +num = c1Orc2 instanceof C2 && c1Orc2.p2; // C2 +str = c1Orc2 instanceof D1 && c1Orc2.p1; // D1 +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