Skip to content

Commit aaa6c4e

Browse files
authored
Add an extra test case for narrowing of union containing a type parameter (#59628)
1 parent a053072 commit aaa6c4e

File tree

3 files changed

+293
-0
lines changed

3 files changed

+293
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//// [tests/cases/compiler/controlFlowUnionContainingTypeParameter1.ts] ////
2+
3+
=== controlFlowUnionContainingTypeParameter1.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/44814
5+
6+
class TestClass<T> {
7+
>TestClass : Symbol(TestClass, Decl(controlFlowUnionContainingTypeParameter1.ts, 0, 0))
8+
>T : Symbol(T, Decl(controlFlowUnionContainingTypeParameter1.ts, 2, 16))
9+
10+
typeguard(val: unknown): val is T {
11+
>typeguard : Symbol(TestClass.typeguard, Decl(controlFlowUnionContainingTypeParameter1.ts, 2, 20))
12+
>val : Symbol(val, Decl(controlFlowUnionContainingTypeParameter1.ts, 3, 12))
13+
>val : Symbol(val, Decl(controlFlowUnionContainingTypeParameter1.ts, 3, 12))
14+
>T : Symbol(T, Decl(controlFlowUnionContainingTypeParameter1.ts, 2, 16))
15+
16+
return true;
17+
}
18+
f(v: number): void {}
19+
>f : Symbol(TestClass.f, Decl(controlFlowUnionContainingTypeParameter1.ts, 5, 3))
20+
>v : Symbol(v, Decl(controlFlowUnionContainingTypeParameter1.ts, 6, 4))
21+
22+
h(v: T): void {}
23+
>h : Symbol(TestClass.h, Decl(controlFlowUnionContainingTypeParameter1.ts, 6, 23))
24+
>v : Symbol(v, Decl(controlFlowUnionContainingTypeParameter1.ts, 7, 4))
25+
>T : Symbol(T, Decl(controlFlowUnionContainingTypeParameter1.ts, 2, 16))
26+
27+
func(val: T | number): void {
28+
>func : Symbol(TestClass.func, Decl(controlFlowUnionContainingTypeParameter1.ts, 7, 18))
29+
>val : Symbol(val, Decl(controlFlowUnionContainingTypeParameter1.ts, 8, 7))
30+
>T : Symbol(T, Decl(controlFlowUnionContainingTypeParameter1.ts, 2, 16))
31+
32+
if (this.typeguard(val)) {
33+
>this.typeguard : Symbol(TestClass.typeguard, Decl(controlFlowUnionContainingTypeParameter1.ts, 2, 20))
34+
>this : Symbol(TestClass, Decl(controlFlowUnionContainingTypeParameter1.ts, 0, 0))
35+
>typeguard : Symbol(TestClass.typeguard, Decl(controlFlowUnionContainingTypeParameter1.ts, 2, 20))
36+
>val : Symbol(val, Decl(controlFlowUnionContainingTypeParameter1.ts, 8, 7))
37+
38+
this.h(val);
39+
>this.h : Symbol(TestClass.h, Decl(controlFlowUnionContainingTypeParameter1.ts, 6, 23))
40+
>this : Symbol(TestClass, Decl(controlFlowUnionContainingTypeParameter1.ts, 0, 0))
41+
>h : Symbol(TestClass.h, Decl(controlFlowUnionContainingTypeParameter1.ts, 6, 23))
42+
>val : Symbol(val, Decl(controlFlowUnionContainingTypeParameter1.ts, 8, 7))
43+
44+
return;
45+
}
46+
this.f(val);
47+
>this.f : Symbol(TestClass.f, Decl(controlFlowUnionContainingTypeParameter1.ts, 5, 3))
48+
>this : Symbol(TestClass, Decl(controlFlowUnionContainingTypeParameter1.ts, 0, 0))
49+
>f : Symbol(TestClass.f, Decl(controlFlowUnionContainingTypeParameter1.ts, 5, 3))
50+
>val : Symbol(val, Decl(controlFlowUnionContainingTypeParameter1.ts, 8, 7))
51+
}
52+
}
53+
54+
class TestClass2<T extends Date> {
55+
>TestClass2 : Symbol(TestClass2, Decl(controlFlowUnionContainingTypeParameter1.ts, 15, 1))
56+
>T : Symbol(T, Decl(controlFlowUnionContainingTypeParameter1.ts, 17, 17))
57+
>Date : Symbol(Date, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.scripthost.d.ts, --, --))
58+
59+
typeguard(val: unknown): val is T {
60+
>typeguard : Symbol(TestClass2.typeguard, Decl(controlFlowUnionContainingTypeParameter1.ts, 17, 34))
61+
>val : Symbol(val, Decl(controlFlowUnionContainingTypeParameter1.ts, 18, 12))
62+
>val : Symbol(val, Decl(controlFlowUnionContainingTypeParameter1.ts, 18, 12))
63+
>T : Symbol(T, Decl(controlFlowUnionContainingTypeParameter1.ts, 17, 17))
64+
65+
return true;
66+
}
67+
f(v: number): void {}
68+
>f : Symbol(TestClass2.f, Decl(controlFlowUnionContainingTypeParameter1.ts, 20, 3))
69+
>v : Symbol(v, Decl(controlFlowUnionContainingTypeParameter1.ts, 21, 4))
70+
71+
h(v: T): void {}
72+
>h : Symbol(TestClass2.h, Decl(controlFlowUnionContainingTypeParameter1.ts, 21, 23))
73+
>v : Symbol(v, Decl(controlFlowUnionContainingTypeParameter1.ts, 22, 4))
74+
>T : Symbol(T, Decl(controlFlowUnionContainingTypeParameter1.ts, 17, 17))
75+
76+
func(val: T | number): void {
77+
>func : Symbol(TestClass2.func, Decl(controlFlowUnionContainingTypeParameter1.ts, 22, 18))
78+
>val : Symbol(val, Decl(controlFlowUnionContainingTypeParameter1.ts, 23, 7))
79+
>T : Symbol(T, Decl(controlFlowUnionContainingTypeParameter1.ts, 17, 17))
80+
81+
if (this.typeguard(val)) {
82+
>this.typeguard : Symbol(TestClass2.typeguard, Decl(controlFlowUnionContainingTypeParameter1.ts, 17, 34))
83+
>this : Symbol(TestClass2, Decl(controlFlowUnionContainingTypeParameter1.ts, 15, 1))
84+
>typeguard : Symbol(TestClass2.typeguard, Decl(controlFlowUnionContainingTypeParameter1.ts, 17, 34))
85+
>val : Symbol(val, Decl(controlFlowUnionContainingTypeParameter1.ts, 23, 7))
86+
87+
this.h(val);
88+
>this.h : Symbol(TestClass2.h, Decl(controlFlowUnionContainingTypeParameter1.ts, 21, 23))
89+
>this : Symbol(TestClass2, Decl(controlFlowUnionContainingTypeParameter1.ts, 15, 1))
90+
>h : Symbol(TestClass2.h, Decl(controlFlowUnionContainingTypeParameter1.ts, 21, 23))
91+
>val : Symbol(val, Decl(controlFlowUnionContainingTypeParameter1.ts, 23, 7))
92+
93+
return;
94+
}
95+
val;
96+
>val : Symbol(val, Decl(controlFlowUnionContainingTypeParameter1.ts, 23, 7))
97+
98+
this.f(val);
99+
>this.f : Symbol(TestClass2.f, Decl(controlFlowUnionContainingTypeParameter1.ts, 20, 3))
100+
>this : Symbol(TestClass2, Decl(controlFlowUnionContainingTypeParameter1.ts, 15, 1))
101+
>f : Symbol(TestClass2.f, Decl(controlFlowUnionContainingTypeParameter1.ts, 20, 3))
102+
>val : Symbol(val, Decl(controlFlowUnionContainingTypeParameter1.ts, 23, 7))
103+
}
104+
}
105+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
//// [tests/cases/compiler/controlFlowUnionContainingTypeParameter1.ts] ////
2+
3+
=== controlFlowUnionContainingTypeParameter1.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/44814
5+
6+
class TestClass<T> {
7+
>TestClass : TestClass<T>
8+
> : ^^^^^^^^^^^^
9+
10+
typeguard(val: unknown): val is T {
11+
>typeguard : (val: unknown) => val is T
12+
> : ^ ^^ ^^^^^
13+
>val : unknown
14+
> : ^^^^^^^
15+
16+
return true;
17+
>true : true
18+
> : ^^^^
19+
}
20+
f(v: number): void {}
21+
>f : (v: number) => void
22+
> : ^ ^^ ^^^^^
23+
>v : number
24+
> : ^^^^^^
25+
26+
h(v: T): void {}
27+
>h : (v: T) => void
28+
> : ^ ^^ ^^^^^
29+
>v : T
30+
> : ^
31+
32+
func(val: T | number): void {
33+
>func : (val: T | number) => void
34+
> : ^ ^^ ^^^^^
35+
>val : number | T
36+
> : ^^^^^^^^^^
37+
38+
if (this.typeguard(val)) {
39+
>this.typeguard(val) : boolean
40+
> : ^^^^^^^
41+
>this.typeguard : (val: unknown) => val is T
42+
> : ^ ^^ ^^^^^
43+
>this : this
44+
> : ^^^^
45+
>typeguard : (val: unknown) => val is T
46+
> : ^ ^^ ^^^^^
47+
>val : number | T
48+
> : ^^^^^^^^^^
49+
50+
this.h(val);
51+
>this.h(val) : void
52+
> : ^^^^
53+
>this.h : (v: T) => void
54+
> : ^ ^^ ^^^^^
55+
>this : this
56+
> : ^^^^
57+
>h : (v: T) => void
58+
> : ^ ^^ ^^^^^
59+
>val : T
60+
> : ^
61+
62+
return;
63+
}
64+
this.f(val);
65+
>this.f(val) : void
66+
> : ^^^^
67+
>this.f : (v: number) => void
68+
> : ^ ^^ ^^^^^
69+
>this : this
70+
> : ^^^^
71+
>f : (v: number) => void
72+
> : ^ ^^ ^^^^^
73+
>val : number
74+
> : ^^^^^^
75+
}
76+
}
77+
78+
class TestClass2<T extends Date> {
79+
>TestClass2 : TestClass2<T>
80+
> : ^^^^^^^^^^^^^
81+
82+
typeguard(val: unknown): val is T {
83+
>typeguard : (val: unknown) => val is T
84+
> : ^ ^^ ^^^^^
85+
>val : unknown
86+
> : ^^^^^^^
87+
88+
return true;
89+
>true : true
90+
> : ^^^^
91+
}
92+
f(v: number): void {}
93+
>f : (v: number) => void
94+
> : ^ ^^ ^^^^^
95+
>v : number
96+
> : ^^^^^^
97+
98+
h(v: T): void {}
99+
>h : (v: T) => void
100+
> : ^ ^^ ^^^^^
101+
>v : T
102+
> : ^
103+
104+
func(val: T | number): void {
105+
>func : (val: T | number) => void
106+
> : ^ ^^ ^^^^^
107+
>val : number | T
108+
> : ^^^^^^^^^^
109+
110+
if (this.typeguard(val)) {
111+
>this.typeguard(val) : boolean
112+
> : ^^^^^^^
113+
>this.typeguard : (val: unknown) => val is T
114+
> : ^ ^^ ^^^^^
115+
>this : this
116+
> : ^^^^
117+
>typeguard : (val: unknown) => val is T
118+
> : ^ ^^ ^^^^^
119+
>val : number | T
120+
> : ^^^^^^^^^^
121+
122+
this.h(val);
123+
>this.h(val) : void
124+
> : ^^^^
125+
>this.h : (v: T) => void
126+
> : ^ ^^ ^^^^^
127+
>this : this
128+
> : ^^^^
129+
>h : (v: T) => void
130+
> : ^ ^^ ^^^^^
131+
>val : T
132+
> : ^
133+
134+
return;
135+
}
136+
val;
137+
>val : number
138+
> : ^^^^^^
139+
140+
this.f(val);
141+
>this.f(val) : void
142+
> : ^^^^
143+
>this.f : (v: number) => void
144+
> : ^ ^^ ^^^^^
145+
>this : this
146+
> : ^^^^
147+
>f : (v: number) => void
148+
> : ^ ^^ ^^^^^
149+
>val : number
150+
> : ^^^^^^
151+
}
152+
}
153+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
// https://github.com/microsoft/TypeScript/issues/44814
5+
6+
class TestClass<T> {
7+
typeguard(val: unknown): val is T {
8+
return true;
9+
}
10+
f(v: number): void {}
11+
h(v: T): void {}
12+
func(val: T | number): void {
13+
if (this.typeguard(val)) {
14+
this.h(val);
15+
return;
16+
}
17+
this.f(val);
18+
}
19+
}
20+
21+
class TestClass2<T extends Date> {
22+
typeguard(val: unknown): val is T {
23+
return true;
24+
}
25+
f(v: number): void {}
26+
h(v: T): void {}
27+
func(val: T | number): void {
28+
if (this.typeguard(val)) {
29+
this.h(val);
30+
return;
31+
}
32+
val;
33+
this.f(val);
34+
}
35+
}

0 commit comments

Comments
 (0)