File tree Expand file tree Collapse file tree 3 files changed +119
-0
lines changed
cases/conformance/expressions/typeGuards Expand file tree Collapse file tree 3 files changed +119
-0
lines changed Original file line number Diff line number Diff line change 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()) { }
Original file line number Diff line number Diff line change 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 ( ) ) { }
Original file line number Diff line number Diff line change 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 ( ) ) { }
You can’t perform that action at this time.
0 commit comments