File tree Expand file tree Collapse file tree 2 files changed +115
-0
lines changed
tests/baselines/reference Expand file tree Collapse file tree 2 files changed +115
-0
lines changed Original file line number Diff line number Diff line change
1
+ tests/cases/conformance/controlFlow/typeGuardsTypeParameters.ts(10,13): error TS2322: Type 'C' is not assignable to type 'T'.
2
+ tests/cases/conformance/controlFlow/typeGuardsTypeParameters.ts(20,11): error TS2339: Property 'length' does not exist on type 'never'.
3
+ tests/cases/conformance/controlFlow/typeGuardsTypeParameters.ts(31,26): error TS2345: Argument of type 'T[keyof T]' is not assignable to parameter of type 'string'.
4
+
5
+
6
+ ==== tests/cases/conformance/controlFlow/typeGuardsTypeParameters.ts (3 errors) ====
7
+
8
+ // Type guards involving type parameters produce intersection types
9
+
10
+ class C {
11
+ prop: string;
12
+ }
13
+
14
+ function f1<T>(x: T) {
15
+ if (x instanceof C) {
16
+ let v1: T = x;
17
+ ~~
18
+ !!! error TS2322: Type 'C' is not assignable to type 'T'.
19
+ let v2: C = x;
20
+ x.prop;
21
+ }
22
+ }
23
+
24
+ function f2<T>(x: T) {
25
+ if (typeof x === "string") {
26
+ let v1: T = x;
27
+ let v2: string = x;
28
+ x.length;
29
+ ~~~~~~
30
+ !!! error TS2339: Property 'length' does not exist on type 'never'.
31
+ }
32
+ }
33
+
34
+ // Repro from #13872
35
+
36
+ function fun<T>(item: { [P in keyof T]: T[P] }) {
37
+ const strings: string[] = [];
38
+ for (const key in item) {
39
+ const value = item[key];
40
+ if (typeof value === "string") {
41
+ strings.push(value);
42
+ ~~~~~
43
+ !!! error TS2345: Argument of type 'T[keyof T]' is not assignable to parameter of type 'string'.
44
+ }
45
+ }
46
+ }
47
+
Original file line number Diff line number Diff line change
1
+ //// [typeGuardsTypeParameters.ts]
2
+
3
+ // Type guards involving type parameters produce intersection types
4
+
5
+ class C {
6
+ prop : string ;
7
+ }
8
+
9
+ function f1 < T > (x: T) {
10
+ if ( x instanceof C ) {
11
+ let v1 : T = x ;
12
+ let v2 : C = x ;
13
+ x . prop ;
14
+ }
15
+ }
16
+
17
+ function f2 < T > (x: T) {
18
+ if ( typeof x === "string" ) {
19
+ let v1 : T = x ;
20
+ let v2 : string = x ;
21
+ x . length ;
22
+ }
23
+ }
24
+
25
+ // Repro from #13872
26
+
27
+ function fun < T > (item: { [ P in keyof T ] : T [ P ] } ) {
28
+ const strings : string [ ] = [ ] ;
29
+ for ( const key in item ) {
30
+ const value = item [ key ] ;
31
+ if ( typeof value === "string" ) {
32
+ strings . push ( value ) ;
33
+ }
34
+ }
35
+ }
36
+
37
+
38
+ //// [typeGuardsTypeParameters.js]
39
+ // Type guards involving type parameters produce intersection types
40
+ var C = ( function ( ) {
41
+ function C ( ) {
42
+ }
43
+ return C ;
44
+ } ( ) ) ;
45
+ function f1 ( x ) {
46
+ if ( x instanceof C ) {
47
+ var v1 = x ;
48
+ var v2 = x ;
49
+ x . prop ;
50
+ }
51
+ }
52
+ function f2 ( x ) {
53
+ if ( typeof x === "string" ) {
54
+ var v1 = x ;
55
+ var v2 = x ;
56
+ x . length ;
57
+ }
58
+ }
59
+ // Repro from #13872
60
+ function fun ( item ) {
61
+ var strings = [ ] ;
62
+ for ( var key in item ) {
63
+ var value = item [ key ] ;
64
+ if ( typeof value === "string" ) {
65
+ strings . push ( value ) ;
66
+ }
67
+ }
68
+ }
You can’t perform that action at this time.
0 commit comments