From 8899d5f056e807d7a2f0c172978dd9a9bada68da Mon Sep 17 00:00:00 2001 From: Yui T Date: Wed, 15 Apr 2015 16:55:59 -0700 Subject: [PATCH 1/2] Add additional tests for typeguard for class or object property --- .../typeGuardsOnClassProperty.errors.txt | 37 +++++++++++++ .../reference/typeGuardsOnClassProperty.js | 54 +++++++++++++++++++ .../typeGuards/typeGuardsOnClassProperty.ts | 28 ++++++++++ 3 files changed, 119 insertions(+) create mode 100644 tests/baselines/reference/typeGuardsOnClassProperty.errors.txt create mode 100644 tests/baselines/reference/typeGuardsOnClassProperty.js create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardsOnClassProperty.ts diff --git a/tests/baselines/reference/typeGuardsOnClassProperty.errors.txt b/tests/baselines/reference/typeGuardsOnClassProperty.errors.txt new file mode 100644 index 0000000000000..7d56c9a204906 --- /dev/null +++ b/tests/baselines/reference/typeGuardsOnClassProperty.errors.txt @@ -0,0 +1,37 @@ +tests/cases/conformance/expressions/typeGuards/typeGuardsOnClassProperty.ts(14,71): error TS2339: Property 'join' does not exist on type 'string | string[]'. +tests/cases/conformance/expressions/typeGuards/typeGuardsOnClassProperty.ts(26,44): error TS2339: Property 'toLowerCase' does not exist on type 'string | number'. + + +==== tests/cases/conformance/expressions/typeGuards/typeGuardsOnClassProperty.ts (2 errors) ==== + // Note that type guards affect types of variables and parameters only and + // have no effect on members of objects such as properties. + + // Note that the class's property must be copied to a local variable for + // the type guard to have an affect + class D { + data: string | string[]; + getData() { + var data = this.data; + return typeof data === "string" ? data : data.join(" "); + } + + getData1() { + return typeof this.data === "string" ? this.data : this. data.join(" "); + ~~~~ +!!! error TS2339: Property 'join' does not exist on type 'string | string[]'. + } + } + + var o: { + prop1: number|string; + prop2: boolean|string; + } = { + prop1: "string" , + prop2: true + } + + if (typeof o.prop1 === "string" && o.prop1.toLowerCase()) {} + ~~~~~~~~~~~ +!!! error TS2339: Property 'toLowerCase' does not exist on type 'string | number'. + var prop1 = o.prop1; + if (typeof prop1 === "string" && prop1.toLocaleLowerCase()) { } \ No newline at end of file diff --git a/tests/baselines/reference/typeGuardsOnClassProperty.js b/tests/baselines/reference/typeGuardsOnClassProperty.js new file mode 100644 index 0000000000000..5edecd36b86d0 --- /dev/null +++ b/tests/baselines/reference/typeGuardsOnClassProperty.js @@ -0,0 +1,54 @@ +//// [typeGuardsOnClassProperty.ts] +// Note that type guards affect types of variables and parameters only and +// have no effect on members of objects such as properties. + +// Note that the class's property must be copied to a local variable for +// the type guard to have an affect +class D { + data: string | string[]; + getData() { + var data = this.data; + return typeof data === "string" ? data : data.join(" "); + } + + getData1() { + return typeof this.data === "string" ? this.data : this. data.join(" "); + } +} + +var o: { + prop1: number|string; + prop2: boolean|string; +} = { + prop1: "string" , + prop2: true + } + +if (typeof o.prop1 === "string" && o.prop1.toLowerCase()) {} +var prop1 = o.prop1; +if (typeof prop1 === "string" && prop1.toLocaleLowerCase()) { } + +//// [typeGuardsOnClassProperty.js] +// Note that type guards affect types of variables and parameters only and +// have no effect on members of objects such as properties. +// Note that the class's property must be copied to a local variable for +// the type guard to have an affect +var D = (function () { + function D() { + } + D.prototype.getData = function () { + var data = this.data; + return typeof data === "string" ? data : data.join(" "); + }; + D.prototype.getData1 = function () { + return typeof this.data === "string" ? this.data : this.data.join(" "); + }; + return D; +})(); +var o = { + prop1: "string", + prop2: true +}; +if (typeof o.prop1 === "string" && o.prop1.toLowerCase()) { } +var prop1 = o.prop1; +if (typeof prop1 === "string" && prop1.toLocaleLowerCase()) { } diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardsOnClassProperty.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardsOnClassProperty.ts new file mode 100644 index 0000000000000..20a0bf8242a23 --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardsOnClassProperty.ts @@ -0,0 +1,28 @@ +// Note that type guards affect types of variables and parameters only and +// have no effect on members of objects such as properties. + +// Note that the class's property must be copied to a local variable for +// the type guard to have an affect +class D { + data: string | string[]; + getData() { + var data = this.data; + return typeof data === "string" ? data : data.join(" "); + } + + getData1() { + return typeof this.data === "string" ? this.data : this. data.join(" "); + } +} + +var o: { + prop1: number|string; + prop2: boolean|string; +} = { + prop1: "string" , + prop2: true + } + +if (typeof o.prop1 === "string" && o.prop1.toLowerCase()) {} +var prop1 = o.prop1; +if (typeof prop1 === "string" && prop1.toLocaleLowerCase()) { } \ No newline at end of file From ac8ae27139462c90a75ba89624589e55bb372120 Mon Sep 17 00:00:00 2001 From: Yui T Date: Thu, 16 Apr 2015 20:09:03 -0700 Subject: [PATCH 2/2] Address code review --- .../reference/typeGuardsOnClassProperty.errors.txt | 8 ++++---- tests/baselines/reference/typeGuardsOnClassProperty.js | 6 +++--- .../expressions/typeGuards/typeGuardsOnClassProperty.ts | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/baselines/reference/typeGuardsOnClassProperty.errors.txt b/tests/baselines/reference/typeGuardsOnClassProperty.errors.txt index 7d56c9a204906..4e6a8b83ae349 100644 --- a/tests/baselines/reference/typeGuardsOnClassProperty.errors.txt +++ b/tests/baselines/reference/typeGuardsOnClassProperty.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/expressions/typeGuards/typeGuardsOnClassProperty.ts(14,71): error TS2339: Property 'join' does not exist on type 'string | string[]'. +tests/cases/conformance/expressions/typeGuards/typeGuardsOnClassProperty.ts(14,70): error TS2339: Property 'join' does not exist on type 'string | string[]'. tests/cases/conformance/expressions/typeGuards/typeGuardsOnClassProperty.ts(26,44): error TS2339: Property 'toLowerCase' does not exist on type 'string | number'. @@ -7,7 +7,7 @@ tests/cases/conformance/expressions/typeGuards/typeGuardsOnClassProperty.ts(26,4 // have no effect on members of objects such as properties. // Note that the class's property must be copied to a local variable for - // the type guard to have an affect + // the type guard to have an effect class D { data: string | string[]; getData() { @@ -16,8 +16,8 @@ tests/cases/conformance/expressions/typeGuards/typeGuardsOnClassProperty.ts(26,4 } getData1() { - return typeof this.data === "string" ? this.data : this. data.join(" "); - ~~~~ + return typeof this.data === "string" ? this.data : this.data.join(" "); + ~~~~ !!! error TS2339: Property 'join' does not exist on type 'string | string[]'. } } diff --git a/tests/baselines/reference/typeGuardsOnClassProperty.js b/tests/baselines/reference/typeGuardsOnClassProperty.js index 5edecd36b86d0..de4fcb50638da 100644 --- a/tests/baselines/reference/typeGuardsOnClassProperty.js +++ b/tests/baselines/reference/typeGuardsOnClassProperty.js @@ -3,7 +3,7 @@ // have no effect on members of objects such as properties. // Note that the class's property must be copied to a local variable for -// the type guard to have an affect +// the type guard to have an effect class D { data: string | string[]; getData() { @@ -12,7 +12,7 @@ class D { } getData1() { - return typeof this.data === "string" ? this.data : this. data.join(" "); + return typeof this.data === "string" ? this.data : this.data.join(" "); } } @@ -32,7 +32,7 @@ if (typeof prop1 === "string" && prop1.toLocaleLowerCase()) { } // Note that type guards affect types of variables and parameters only and // have no effect on members of objects such as properties. // Note that the class's property must be copied to a local variable for -// the type guard to have an affect +// the type guard to have an effect var D = (function () { function D() { } diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardsOnClassProperty.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardsOnClassProperty.ts index 20a0bf8242a23..2c26b10c6a1b0 100644 --- a/tests/cases/conformance/expressions/typeGuards/typeGuardsOnClassProperty.ts +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardsOnClassProperty.ts @@ -2,7 +2,7 @@ // have no effect on members of objects such as properties. // Note that the class's property must be copied to a local variable for -// the type guard to have an affect +// the type guard to have an effect class D { data: string | string[]; getData() { @@ -11,7 +11,7 @@ class D { } getData1() { - return typeof this.data === "string" ? this.data : this. data.join(" "); + return typeof this.data === "string" ? this.data : this.data.join(" "); } }