Skip to content

Commit ae63c73

Browse files
committed
Add tests
1 parent e2aa5c6 commit ae63c73

File tree

4 files changed

+240
-68
lines changed

4 files changed

+240
-68
lines changed

tests/baselines/reference/narrowingMutualSubtypes.js

+51-7
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ const c4 = {};
2727
const a4a = [c4, r4]; // {}[]
2828
const a4b = [r4, c4]; // {}[]
2929

30-
// Check that narrowing preserves original type in false branch for non-identical mutual subtypes
30+
// Check that {} is a strict supertype of Record<string, unknown>
3131

3232
declare function isObject1(value: unknown): value is Record<string, unknown>;
3333

34-
function gg(x: {}) {
34+
function gg1(x: {}) {
3535
if (isObject1(x)) {
3636
x; // Record<string, unknown>
3737
}
@@ -45,14 +45,40 @@ declare function isObject2(value: unknown): value is {};
4545

4646
function gg2(x: Record<string, unknown>) {
4747
if (isObject2(x)) {
48-
x; // {}
48+
x; // Record<string, unknown>
4949
}
5050
else {
51-
x; // Record<string, unknown>
51+
x; // never
5252
}
5353
x; // Record<string, unknown>
5454
}
5555

56+
// Check that {} is a strict supertype of Record<string, any>
57+
58+
declare function isObject3(value: unknown): value is Record<string, any>;
59+
60+
function gg3(x: {}) {
61+
if (isObject3(x)) {
62+
x; // Record<string, any>
63+
}
64+
else {
65+
x; // {}
66+
}
67+
x; // {}
68+
}
69+
70+
declare function isObject4(value: unknown): value is {};
71+
72+
function gg4(x: Record<string, any>) {
73+
if (isObject4(x)) {
74+
x; // Record<string, any>
75+
}
76+
else {
77+
x; // never
78+
}
79+
x; // Record<string, any>
80+
}
81+
5682
// Repro from #50916
5783

5884
type Identity<T> = {[K in keyof T]: T[K]};
@@ -92,7 +118,7 @@ var a3b = [r3, c3]; // {}[]
92118
var c4 = {};
93119
var a4a = [c4, r4]; // {}[]
94120
var a4b = [r4, c4]; // {}[]
95-
function gg(x) {
121+
function gg1(x) {
96122
if (isObject1(x)) {
97123
x; // Record<string, unknown>
98124
}
@@ -103,13 +129,31 @@ function gg(x) {
103129
}
104130
function gg2(x) {
105131
if (isObject2(x)) {
106-
x; // {}
132+
x; // Record<string, unknown>
107133
}
108134
else {
109-
x; // Record<string, unknown>
135+
x; // never
110136
}
111137
x; // Record<string, unknown>
112138
}
139+
function gg3(x) {
140+
if (isObject3(x)) {
141+
x; // Record<string, any>
142+
}
143+
else {
144+
x; // {}
145+
}
146+
x; // {}
147+
}
148+
function gg4(x) {
149+
if (isObject4(x)) {
150+
x; // Record<string, any>
151+
}
152+
else {
153+
x; // never
154+
}
155+
x; // Record<string, any>
156+
}
113157
function is(value) {
114158
return true;
115159
}

tests/baselines/reference/narrowingMutualSubtypes.symbols

+104-52
Original file line numberDiff line numberDiff line change
@@ -73,31 +73,31 @@ const a4b = [r4, c4]; // {}[]
7373
>r4 : Symbol(r4, Decl(narrowingMutualSubtypes.ts, 22, 13))
7474
>c4 : Symbol(c4, Decl(narrowingMutualSubtypes.ts, 23, 5))
7575

76-
// Check that narrowing preserves original type in false branch for non-identical mutual subtypes
76+
// Check that {} is a strict supertype of Record<string, unknown>
7777

7878
declare function isObject1(value: unknown): value is Record<string, unknown>;
7979
>isObject1 : Symbol(isObject1, Decl(narrowingMutualSubtypes.ts, 26, 21))
8080
>value : Symbol(value, Decl(narrowingMutualSubtypes.ts, 30, 27))
8181
>value : Symbol(value, Decl(narrowingMutualSubtypes.ts, 30, 27))
8282
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
8383

84-
function gg(x: {}) {
85-
>gg : Symbol(gg, Decl(narrowingMutualSubtypes.ts, 30, 77))
86-
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 32, 12))
84+
function gg1(x: {}) {
85+
>gg1 : Symbol(gg1, Decl(narrowingMutualSubtypes.ts, 30, 77))
86+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 32, 13))
8787

8888
if (isObject1(x)) {
8989
>isObject1 : Symbol(isObject1, Decl(narrowingMutualSubtypes.ts, 26, 21))
90-
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 32, 12))
90+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 32, 13))
9191

9292
x; // Record<string, unknown>
93-
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 32, 12))
93+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 32, 13))
9494
}
9595
else {
9696
x; // {}
97-
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 32, 12))
97+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 32, 13))
9898
}
9999
x; // {}
100-
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 32, 12))
100+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 32, 13))
101101
}
102102

103103
declare function isObject2(value: unknown): value is {};
@@ -114,90 +114,142 @@ function gg2(x: Record<string, unknown>) {
114114
>isObject2 : Symbol(isObject2, Decl(narrowingMutualSubtypes.ts, 40, 1))
115115
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 44, 13))
116116

117-
x; // {}
117+
x; // Record<string, unknown>
118118
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 44, 13))
119119
}
120120
else {
121-
x; // Record<string, unknown>
121+
x; // never
122122
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 44, 13))
123123
}
124124
x; // Record<string, unknown>
125125
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 44, 13))
126126
}
127127

128+
// Check that {} is a strict supertype of Record<string, any>
129+
130+
declare function isObject3(value: unknown): value is Record<string, any>;
131+
>isObject3 : Symbol(isObject3, Decl(narrowingMutualSubtypes.ts, 52, 1))
132+
>value : Symbol(value, Decl(narrowingMutualSubtypes.ts, 56, 27))
133+
>value : Symbol(value, Decl(narrowingMutualSubtypes.ts, 56, 27))
134+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
135+
136+
function gg3(x: {}) {
137+
>gg3 : Symbol(gg3, Decl(narrowingMutualSubtypes.ts, 56, 73))
138+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 58, 13))
139+
140+
if (isObject3(x)) {
141+
>isObject3 : Symbol(isObject3, Decl(narrowingMutualSubtypes.ts, 52, 1))
142+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 58, 13))
143+
144+
x; // Record<string, any>
145+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 58, 13))
146+
}
147+
else {
148+
x; // {}
149+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 58, 13))
150+
}
151+
x; // {}
152+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 58, 13))
153+
}
154+
155+
declare function isObject4(value: unknown): value is {};
156+
>isObject4 : Symbol(isObject4, Decl(narrowingMutualSubtypes.ts, 66, 1))
157+
>value : Symbol(value, Decl(narrowingMutualSubtypes.ts, 68, 27))
158+
>value : Symbol(value, Decl(narrowingMutualSubtypes.ts, 68, 27))
159+
160+
function gg4(x: Record<string, any>) {
161+
>gg4 : Symbol(gg4, Decl(narrowingMutualSubtypes.ts, 68, 56))
162+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 70, 13))
163+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
164+
165+
if (isObject4(x)) {
166+
>isObject4 : Symbol(isObject4, Decl(narrowingMutualSubtypes.ts, 66, 1))
167+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 70, 13))
168+
169+
x; // Record<string, any>
170+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 70, 13))
171+
}
172+
else {
173+
x; // never
174+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 70, 13))
175+
}
176+
x; // Record<string, any>
177+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 70, 13))
178+
}
179+
128180
// Repro from #50916
129181

130182
type Identity<T> = {[K in keyof T]: T[K]};
131-
>Identity : Symbol(Identity, Decl(narrowingMutualSubtypes.ts, 52, 1))
132-
>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 56, 14))
133-
>K : Symbol(K, Decl(narrowingMutualSubtypes.ts, 56, 21))
134-
>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 56, 14))
135-
>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 56, 14))
136-
>K : Symbol(K, Decl(narrowingMutualSubtypes.ts, 56, 21))
183+
>Identity : Symbol(Identity, Decl(narrowingMutualSubtypes.ts, 78, 1))
184+
>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 82, 14))
185+
>K : Symbol(K, Decl(narrowingMutualSubtypes.ts, 82, 21))
186+
>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 82, 14))
187+
>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 82, 14))
188+
>K : Symbol(K, Decl(narrowingMutualSubtypes.ts, 82, 21))
137189

138190
type Self<T> = T extends unknown ? Identity<T> : never;
139-
>Self : Symbol(Self, Decl(narrowingMutualSubtypes.ts, 56, 42))
140-
>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 58, 10))
141-
>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 58, 10))
142-
>Identity : Symbol(Identity, Decl(narrowingMutualSubtypes.ts, 52, 1))
143-
>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 58, 10))
191+
>Self : Symbol(Self, Decl(narrowingMutualSubtypes.ts, 82, 42))
192+
>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 84, 10))
193+
>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 84, 10))
194+
>Identity : Symbol(Identity, Decl(narrowingMutualSubtypes.ts, 78, 1))
195+
>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 84, 10))
144196

145197
function is<T>(value: T): value is Self<T> {
146-
>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 58, 55))
147-
>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 60, 12))
148-
>value : Symbol(value, Decl(narrowingMutualSubtypes.ts, 60, 15))
149-
>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 60, 12))
150-
>value : Symbol(value, Decl(narrowingMutualSubtypes.ts, 60, 15))
151-
>Self : Symbol(Self, Decl(narrowingMutualSubtypes.ts, 56, 42))
152-
>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 60, 12))
198+
>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 84, 55))
199+
>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 86, 12))
200+
>value : Symbol(value, Decl(narrowingMutualSubtypes.ts, 86, 15))
201+
>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 86, 12))
202+
>value : Symbol(value, Decl(narrowingMutualSubtypes.ts, 86, 15))
203+
>Self : Symbol(Self, Decl(narrowingMutualSubtypes.ts, 82, 42))
204+
>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 86, 12))
153205

154206
return true;
155207
}
156208

157209
type Union = {a: number} | {b: number} | {c: number};
158-
>Union : Symbol(Union, Decl(narrowingMutualSubtypes.ts, 62, 1))
159-
>a : Symbol(a, Decl(narrowingMutualSubtypes.ts, 64, 15))
160-
>b : Symbol(b, Decl(narrowingMutualSubtypes.ts, 64, 29))
161-
>c : Symbol(c, Decl(narrowingMutualSubtypes.ts, 64, 43))
210+
>Union : Symbol(Union, Decl(narrowingMutualSubtypes.ts, 88, 1))
211+
>a : Symbol(a, Decl(narrowingMutualSubtypes.ts, 90, 15))
212+
>b : Symbol(b, Decl(narrowingMutualSubtypes.ts, 90, 29))
213+
>c : Symbol(c, Decl(narrowingMutualSubtypes.ts, 90, 43))
162214

163215
function example(x: Union) {
164-
>example : Symbol(example, Decl(narrowingMutualSubtypes.ts, 64, 54))
165-
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 66, 17))
166-
>Union : Symbol(Union, Decl(narrowingMutualSubtypes.ts, 62, 1))
216+
>example : Symbol(example, Decl(narrowingMutualSubtypes.ts, 90, 54))
217+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 92, 17))
218+
>Union : Symbol(Union, Decl(narrowingMutualSubtypes.ts, 88, 1))
167219

168220
if (is(x)) {}
169-
>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 58, 55))
170-
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 66, 17))
221+
>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 84, 55))
222+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 92, 17))
171223

172224
if (is(x)) {}
173-
>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 58, 55))
174-
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 66, 17))
225+
>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 84, 55))
226+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 92, 17))
175227

176228
if (is(x)) {}
177-
>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 58, 55))
178-
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 66, 17))
229+
>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 84, 55))
230+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 92, 17))
179231

180232
if (is(x)) {}
181-
>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 58, 55))
182-
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 66, 17))
233+
>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 84, 55))
234+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 92, 17))
183235

184236
if (is(x)) {}
185-
>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 58, 55))
186-
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 66, 17))
237+
>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 84, 55))
238+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 92, 17))
187239

188240
if (is(x)) {}
189-
>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 58, 55))
190-
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 66, 17))
241+
>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 84, 55))
242+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 92, 17))
191243

192244
if (is(x)) {}
193-
>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 58, 55))
194-
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 66, 17))
245+
>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 84, 55))
246+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 92, 17))
195247

196248
if (is(x)) {}
197-
>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 58, 55))
198-
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 66, 17))
249+
>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 84, 55))
250+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 92, 17))
199251

200252
x; // Union
201-
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 66, 17))
253+
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 92, 17))
202254
}
203255

0 commit comments

Comments
 (0)