Skip to content

Commit 238067e

Browse files
committed
Add tests
1 parent 3d069f7 commit 238067e

File tree

4 files changed

+638
-0
lines changed

4 files changed

+638
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
//// [typeVariableTypeGuards.ts]
2+
// Repro from #14091
3+
4+
interface Foo {
5+
foo(): void
6+
}
7+
8+
class A<P extends Partial<Foo>> {
9+
props: Readonly<P>
10+
doSomething() {
11+
this.props.foo && this.props.foo()
12+
}
13+
}
14+
15+
// Repro from #14415
16+
17+
interface Banana {
18+
color: 'yellow';
19+
}
20+
21+
class Monkey<T extends Banana | undefined> {
22+
a: T;
23+
render() {
24+
if (this.a) {
25+
this.a.color;
26+
}
27+
}
28+
}
29+
30+
interface BigBanana extends Banana {
31+
}
32+
33+
class BigMonkey extends Monkey<BigBanana> {
34+
render() {
35+
if (this.a) {
36+
this.a.color;
37+
}
38+
}
39+
}
40+
41+
// Another repro
42+
43+
type Item = {
44+
(): string;
45+
x: string;
46+
}
47+
48+
function f1<T extends Item | undefined>(obj: T) {
49+
if (obj) {
50+
obj.x;
51+
obj["x"];
52+
obj();
53+
}
54+
}
55+
56+
function f2<T extends Item | undefined>(obj: T | undefined) {
57+
if (obj) {
58+
obj.x;
59+
obj["x"];
60+
obj();
61+
}
62+
}
63+
64+
function f3<T extends Item | undefined>(obj: T | null) {
65+
if (obj) {
66+
obj.x;
67+
obj["x"];
68+
obj();
69+
}
70+
}
71+
72+
function f4<T extends string[] | undefined>(obj: T | undefined, x: number) {
73+
if (obj) {
74+
obj[x].length;
75+
}
76+
}
77+
78+
79+
//// [typeVariableTypeGuards.js]
80+
"use strict";
81+
// Repro from #14091
82+
var __extends = (this && this.__extends) || (function () {
83+
var extendStatics = Object.setPrototypeOf ||
84+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
85+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
86+
return function (d, b) {
87+
extendStatics(d, b);
88+
function __() { this.constructor = d; }
89+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
90+
};
91+
})();
92+
var A = (function () {
93+
function A() {
94+
}
95+
A.prototype.doSomething = function () {
96+
this.props.foo && this.props.foo();
97+
};
98+
return A;
99+
}());
100+
var Monkey = (function () {
101+
function Monkey() {
102+
}
103+
Monkey.prototype.render = function () {
104+
if (this.a) {
105+
this.a.color;
106+
}
107+
};
108+
return Monkey;
109+
}());
110+
var BigMonkey = (function (_super) {
111+
__extends(BigMonkey, _super);
112+
function BigMonkey() {
113+
return _super !== null && _super.apply(this, arguments) || this;
114+
}
115+
BigMonkey.prototype.render = function () {
116+
if (this.a) {
117+
this.a.color;
118+
}
119+
};
120+
return BigMonkey;
121+
}(Monkey));
122+
function f1(obj) {
123+
if (obj) {
124+
obj.x;
125+
obj["x"];
126+
obj();
127+
}
128+
}
129+
function f2(obj) {
130+
if (obj) {
131+
obj.x;
132+
obj["x"];
133+
obj();
134+
}
135+
}
136+
function f3(obj) {
137+
if (obj) {
138+
obj.x;
139+
obj["x"];
140+
obj();
141+
}
142+
}
143+
function f4(obj, x) {
144+
if (obj) {
145+
obj[x].length;
146+
}
147+
}
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
=== tests/cases/compiler/typeVariableTypeGuards.ts ===
2+
// Repro from #14091
3+
4+
interface Foo {
5+
>Foo : Symbol(Foo, Decl(typeVariableTypeGuards.ts, 0, 0))
6+
7+
foo(): void
8+
>foo : Symbol(Foo.foo, Decl(typeVariableTypeGuards.ts, 2, 15))
9+
}
10+
11+
class A<P extends Partial<Foo>> {
12+
>A : Symbol(A, Decl(typeVariableTypeGuards.ts, 4, 1))
13+
>P : Symbol(P, Decl(typeVariableTypeGuards.ts, 6, 8))
14+
>Partial : Symbol(Partial, Decl(lib.d.ts, --, --))
15+
>Foo : Symbol(Foo, Decl(typeVariableTypeGuards.ts, 0, 0))
16+
17+
props: Readonly<P>
18+
>props : Symbol(A.props, Decl(typeVariableTypeGuards.ts, 6, 33))
19+
>Readonly : Symbol(Readonly, Decl(lib.d.ts, --, --))
20+
>P : Symbol(P, Decl(typeVariableTypeGuards.ts, 6, 8))
21+
22+
doSomething() {
23+
>doSomething : Symbol(A.doSomething, Decl(typeVariableTypeGuards.ts, 7, 22))
24+
25+
this.props.foo && this.props.foo()
26+
>this.props.foo : Symbol(foo)
27+
>this.props : Symbol(A.props, Decl(typeVariableTypeGuards.ts, 6, 33))
28+
>this : Symbol(A, Decl(typeVariableTypeGuards.ts, 4, 1))
29+
>props : Symbol(A.props, Decl(typeVariableTypeGuards.ts, 6, 33))
30+
>foo : Symbol(foo)
31+
>this.props.foo : Symbol(foo)
32+
>this.props : Symbol(A.props, Decl(typeVariableTypeGuards.ts, 6, 33))
33+
>this : Symbol(A, Decl(typeVariableTypeGuards.ts, 4, 1))
34+
>props : Symbol(A.props, Decl(typeVariableTypeGuards.ts, 6, 33))
35+
>foo : Symbol(foo)
36+
}
37+
}
38+
39+
// Repro from #14415
40+
41+
interface Banana {
42+
>Banana : Symbol(Banana, Decl(typeVariableTypeGuards.ts, 11, 1))
43+
44+
color: 'yellow';
45+
>color : Symbol(Banana.color, Decl(typeVariableTypeGuards.ts, 15, 18))
46+
}
47+
48+
class Monkey<T extends Banana | undefined> {
49+
>Monkey : Symbol(Monkey, Decl(typeVariableTypeGuards.ts, 17, 1))
50+
>T : Symbol(T, Decl(typeVariableTypeGuards.ts, 19, 13))
51+
>Banana : Symbol(Banana, Decl(typeVariableTypeGuards.ts, 11, 1))
52+
53+
a: T;
54+
>a : Symbol(Monkey.a, Decl(typeVariableTypeGuards.ts, 19, 44))
55+
>T : Symbol(T, Decl(typeVariableTypeGuards.ts, 19, 13))
56+
57+
render() {
58+
>render : Symbol(Monkey.render, Decl(typeVariableTypeGuards.ts, 20, 9))
59+
60+
if (this.a) {
61+
>this.a : Symbol(Monkey.a, Decl(typeVariableTypeGuards.ts, 19, 44))
62+
>this : Symbol(Monkey, Decl(typeVariableTypeGuards.ts, 17, 1))
63+
>a : Symbol(Monkey.a, Decl(typeVariableTypeGuards.ts, 19, 44))
64+
65+
this.a.color;
66+
>this.a.color : Symbol(Banana.color, Decl(typeVariableTypeGuards.ts, 15, 18))
67+
>this.a : Symbol(Monkey.a, Decl(typeVariableTypeGuards.ts, 19, 44))
68+
>this : Symbol(Monkey, Decl(typeVariableTypeGuards.ts, 17, 1))
69+
>a : Symbol(Monkey.a, Decl(typeVariableTypeGuards.ts, 19, 44))
70+
>color : Symbol(Banana.color, Decl(typeVariableTypeGuards.ts, 15, 18))
71+
}
72+
}
73+
}
74+
75+
interface BigBanana extends Banana {
76+
>BigBanana : Symbol(BigBanana, Decl(typeVariableTypeGuards.ts, 26, 1))
77+
>Banana : Symbol(Banana, Decl(typeVariableTypeGuards.ts, 11, 1))
78+
}
79+
80+
class BigMonkey extends Monkey<BigBanana> {
81+
>BigMonkey : Symbol(BigMonkey, Decl(typeVariableTypeGuards.ts, 29, 1))
82+
>Monkey : Symbol(Monkey, Decl(typeVariableTypeGuards.ts, 17, 1))
83+
>BigBanana : Symbol(BigBanana, Decl(typeVariableTypeGuards.ts, 26, 1))
84+
85+
render() {
86+
>render : Symbol(BigMonkey.render, Decl(typeVariableTypeGuards.ts, 31, 43))
87+
88+
if (this.a) {
89+
>this.a : Symbol(Monkey.a, Decl(typeVariableTypeGuards.ts, 19, 44))
90+
>this : Symbol(BigMonkey, Decl(typeVariableTypeGuards.ts, 29, 1))
91+
>a : Symbol(Monkey.a, Decl(typeVariableTypeGuards.ts, 19, 44))
92+
93+
this.a.color;
94+
>this.a.color : Symbol(Banana.color, Decl(typeVariableTypeGuards.ts, 15, 18))
95+
>this.a : Symbol(Monkey.a, Decl(typeVariableTypeGuards.ts, 19, 44))
96+
>this : Symbol(BigMonkey, Decl(typeVariableTypeGuards.ts, 29, 1))
97+
>a : Symbol(Monkey.a, Decl(typeVariableTypeGuards.ts, 19, 44))
98+
>color : Symbol(Banana.color, Decl(typeVariableTypeGuards.ts, 15, 18))
99+
}
100+
}
101+
}
102+
103+
// Another repro
104+
105+
type Item = {
106+
>Item : Symbol(Item, Decl(typeVariableTypeGuards.ts, 37, 1))
107+
108+
(): string;
109+
x: string;
110+
>x : Symbol(x, Decl(typeVariableTypeGuards.ts, 42, 15))
111+
}
112+
113+
function f1<T extends Item | undefined>(obj: T) {
114+
>f1 : Symbol(f1, Decl(typeVariableTypeGuards.ts, 44, 1))
115+
>T : Symbol(T, Decl(typeVariableTypeGuards.ts, 46, 12))
116+
>Item : Symbol(Item, Decl(typeVariableTypeGuards.ts, 37, 1))
117+
>obj : Symbol(obj, Decl(typeVariableTypeGuards.ts, 46, 40))
118+
>T : Symbol(T, Decl(typeVariableTypeGuards.ts, 46, 12))
119+
120+
if (obj) {
121+
>obj : Symbol(obj, Decl(typeVariableTypeGuards.ts, 46, 40))
122+
123+
obj.x;
124+
>obj.x : Symbol(x, Decl(typeVariableTypeGuards.ts, 42, 15))
125+
>obj : Symbol(obj, Decl(typeVariableTypeGuards.ts, 46, 40))
126+
>x : Symbol(x, Decl(typeVariableTypeGuards.ts, 42, 15))
127+
128+
obj["x"];
129+
>obj : Symbol(obj, Decl(typeVariableTypeGuards.ts, 46, 40))
130+
>"x" : Symbol(x, Decl(typeVariableTypeGuards.ts, 42, 15))
131+
132+
obj();
133+
>obj : Symbol(obj, Decl(typeVariableTypeGuards.ts, 46, 40))
134+
}
135+
}
136+
137+
function f2<T extends Item | undefined>(obj: T | undefined) {
138+
>f2 : Symbol(f2, Decl(typeVariableTypeGuards.ts, 52, 1))
139+
>T : Symbol(T, Decl(typeVariableTypeGuards.ts, 54, 12))
140+
>Item : Symbol(Item, Decl(typeVariableTypeGuards.ts, 37, 1))
141+
>obj : Symbol(obj, Decl(typeVariableTypeGuards.ts, 54, 40))
142+
>T : Symbol(T, Decl(typeVariableTypeGuards.ts, 54, 12))
143+
144+
if (obj) {
145+
>obj : Symbol(obj, Decl(typeVariableTypeGuards.ts, 54, 40))
146+
147+
obj.x;
148+
>obj.x : Symbol(x, Decl(typeVariableTypeGuards.ts, 42, 15))
149+
>obj : Symbol(obj, Decl(typeVariableTypeGuards.ts, 54, 40))
150+
>x : Symbol(x, Decl(typeVariableTypeGuards.ts, 42, 15))
151+
152+
obj["x"];
153+
>obj : Symbol(obj, Decl(typeVariableTypeGuards.ts, 54, 40))
154+
>"x" : Symbol(x, Decl(typeVariableTypeGuards.ts, 42, 15))
155+
156+
obj();
157+
>obj : Symbol(obj, Decl(typeVariableTypeGuards.ts, 54, 40))
158+
}
159+
}
160+
161+
function f3<T extends Item | undefined>(obj: T | null) {
162+
>f3 : Symbol(f3, Decl(typeVariableTypeGuards.ts, 60, 1))
163+
>T : Symbol(T, Decl(typeVariableTypeGuards.ts, 62, 12))
164+
>Item : Symbol(Item, Decl(typeVariableTypeGuards.ts, 37, 1))
165+
>obj : Symbol(obj, Decl(typeVariableTypeGuards.ts, 62, 40))
166+
>T : Symbol(T, Decl(typeVariableTypeGuards.ts, 62, 12))
167+
168+
if (obj) {
169+
>obj : Symbol(obj, Decl(typeVariableTypeGuards.ts, 62, 40))
170+
171+
obj.x;
172+
>obj.x : Symbol(x, Decl(typeVariableTypeGuards.ts, 42, 15))
173+
>obj : Symbol(obj, Decl(typeVariableTypeGuards.ts, 62, 40))
174+
>x : Symbol(x, Decl(typeVariableTypeGuards.ts, 42, 15))
175+
176+
obj["x"];
177+
>obj : Symbol(obj, Decl(typeVariableTypeGuards.ts, 62, 40))
178+
>"x" : Symbol(x, Decl(typeVariableTypeGuards.ts, 42, 15))
179+
180+
obj();
181+
>obj : Symbol(obj, Decl(typeVariableTypeGuards.ts, 62, 40))
182+
}
183+
}
184+
185+
function f4<T extends string[] | undefined>(obj: T | undefined, x: number) {
186+
>f4 : Symbol(f4, Decl(typeVariableTypeGuards.ts, 68, 1))
187+
>T : Symbol(T, Decl(typeVariableTypeGuards.ts, 70, 12))
188+
>obj : Symbol(obj, Decl(typeVariableTypeGuards.ts, 70, 44))
189+
>T : Symbol(T, Decl(typeVariableTypeGuards.ts, 70, 12))
190+
>x : Symbol(x, Decl(typeVariableTypeGuards.ts, 70, 63))
191+
192+
if (obj) {
193+
>obj : Symbol(obj, Decl(typeVariableTypeGuards.ts, 70, 44))
194+
195+
obj[x].length;
196+
>obj[x].length : Symbol(String.length, Decl(lib.d.ts, --, --))
197+
>obj : Symbol(obj, Decl(typeVariableTypeGuards.ts, 70, 44))
198+
>x : Symbol(x, Decl(typeVariableTypeGuards.ts, 70, 63))
199+
>length : Symbol(String.length, Decl(lib.d.ts, --, --))
200+
}
201+
}
202+

0 commit comments

Comments
 (0)