Skip to content

Commit 031c344

Browse files
committed
Merge pull request #2785 from Microsoft/conformanceTypeGuard
Conformance for spec update section 4.20 Add additional tests for typeguard for class or object property
2 parents 7a167f2 + bcbcc3f commit 031c344

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
tests/cases/conformance/expressions/typeGuards/typeGuardsOnClassProperty.ts(14,70): error TS2339: Property 'join' does not exist on type 'string | string[]'.
2+
tests/cases/conformance/expressions/typeGuards/typeGuardsOnClassProperty.ts(26,44): error TS2339: Property 'toLowerCase' does not exist on type 'string | number'.
3+
4+
5+
==== tests/cases/conformance/expressions/typeGuards/typeGuardsOnClassProperty.ts (2 errors) ====
6+
// Note that type guards affect types of variables and parameters only and
7+
// have no effect on members of objects such as properties.
8+
9+
// Note that the class's property must be copied to a local variable for
10+
// the type guard to have an effect
11+
class D {
12+
data: string | string[];
13+
getData() {
14+
var data = this.data;
15+
return typeof data === "string" ? data : data.join(" ");
16+
}
17+
18+
getData1() {
19+
return typeof this.data === "string" ? this.data : this.data.join(" ");
20+
~~~~
21+
!!! error TS2339: Property 'join' does not exist on type 'string | string[]'.
22+
}
23+
}
24+
25+
var o: {
26+
prop1: number|string;
27+
prop2: boolean|string;
28+
} = {
29+
prop1: "string" ,
30+
prop2: true
31+
}
32+
33+
if (typeof o.prop1 === "string" && o.prop1.toLowerCase()) {}
34+
~~~~~~~~~~~
35+
!!! error TS2339: Property 'toLowerCase' does not exist on type 'string | number'.
36+
var prop1 = o.prop1;
37+
if (typeof prop1 === "string" && prop1.toLocaleLowerCase()) { }
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//// [typeGuardsOnClassProperty.ts]
2+
// Note that type guards affect types of variables and parameters only and
3+
// have no effect on members of objects such as properties.
4+
5+
// Note that the class's property must be copied to a local variable for
6+
// the type guard to have an effect
7+
class D {
8+
data: string | string[];
9+
getData() {
10+
var data = this.data;
11+
return typeof data === "string" ? data : data.join(" ");
12+
}
13+
14+
getData1() {
15+
return typeof this.data === "string" ? this.data : this.data.join(" ");
16+
}
17+
}
18+
19+
var o: {
20+
prop1: number|string;
21+
prop2: boolean|string;
22+
} = {
23+
prop1: "string" ,
24+
prop2: true
25+
}
26+
27+
if (typeof o.prop1 === "string" && o.prop1.toLowerCase()) {}
28+
var prop1 = o.prop1;
29+
if (typeof prop1 === "string" && prop1.toLocaleLowerCase()) { }
30+
31+
//// [typeGuardsOnClassProperty.js]
32+
// Note that type guards affect types of variables and parameters only and
33+
// have no effect on members of objects such as properties.
34+
// Note that the class's property must be copied to a local variable for
35+
// the type guard to have an effect
36+
var D = (function () {
37+
function D() {
38+
}
39+
D.prototype.getData = function () {
40+
var data = this.data;
41+
return typeof data === "string" ? data : data.join(" ");
42+
};
43+
D.prototype.getData1 = function () {
44+
return typeof this.data === "string" ? this.data : this.data.join(" ");
45+
};
46+
return D;
47+
})();
48+
var o = {
49+
prop1: "string",
50+
prop2: true
51+
};
52+
if (typeof o.prop1 === "string" && o.prop1.toLowerCase()) { }
53+
var prop1 = o.prop1;
54+
if (typeof prop1 === "string" && prop1.toLocaleLowerCase()) { }
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Note that type guards affect types of variables and parameters only and
2+
// have no effect on members of objects such as properties.
3+
4+
// Note that the class's property must be copied to a local variable for
5+
// the type guard to have an effect
6+
class D {
7+
data: string | string[];
8+
getData() {
9+
var data = this.data;
10+
return typeof data === "string" ? data : data.join(" ");
11+
}
12+
13+
getData1() {
14+
return typeof this.data === "string" ? this.data : this.data.join(" ");
15+
}
16+
}
17+
18+
var o: {
19+
prop1: number|string;
20+
prop2: boolean|string;
21+
} = {
22+
prop1: "string" ,
23+
prop2: true
24+
}
25+
26+
if (typeof o.prop1 === "string" && o.prop1.toLowerCase()) {}
27+
var prop1 = o.prop1;
28+
if (typeof prop1 === "string" && prop1.toLocaleLowerCase()) { }

0 commit comments

Comments
 (0)