Skip to content

Commit f885181

Browse files
committed
Add tests
1 parent f7b22a0 commit f885181

File tree

1 file changed

+109
-16
lines changed

1 file changed

+109
-16
lines changed

tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts

Lines changed: 109 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -162,79 +162,172 @@ function f01(x: unknown) {
162162
}
163163
}
164164

165-
type Thing = { foo: number, bar(): number };
165+
type Thing = { foo: string | number, bar(): number, baz: object };
166166

167167
function f10(o: Thing | undefined, value: number) {
168168
if (o?.foo === value) {
169-
o;
170169
o.foo;
171170
}
172171
if (o?.["foo"] === value) {
173-
o;
174172
o["foo"];
175173
}
176174
if (o?.bar() === value) {
177-
o;
175+
o.bar;
176+
}
177+
if (o?.foo == value) {
178+
o.foo;
179+
}
180+
if (o?.["foo"] == value) {
181+
o["foo"];
182+
}
183+
if (o?.bar() == value) {
178184
o.bar;
179185
}
180186
}
181187

182188
function f11(o: Thing | null, value: number) {
183189
if (o?.foo === value) {
184-
o;
185190
o.foo;
186191
}
187192
if (o?.["foo"] === value) {
188-
o;
189193
o["foo"];
190194
}
191195
if (o?.bar() === value) {
192-
o;
196+
o.bar;
197+
}
198+
if (o?.foo == value) {
199+
o.foo;
200+
}
201+
if (o?.["foo"] == value) {
202+
o["foo"];
203+
}
204+
if (o?.bar() == value) {
193205
o.bar;
194206
}
195207
}
196208

197209
function f12(o: Thing | undefined, value: number | undefined) {
198210
if (o?.foo === value) {
199-
o;
200211
o.foo; // Error
201212
}
202213
if (o?.["foo"] === value) {
203-
o;
204214
o["foo"]; // Error
205215
}
206216
if (o?.bar() === value) {
207-
o;
217+
o.bar; // Error
218+
}
219+
if (o?.foo == value) {
220+
o.foo; // Error
221+
}
222+
if (o?.["foo"] == value) {
223+
o["foo"]; // Error
224+
}
225+
if (o?.bar() == value) {
226+
o.bar; // Error
227+
}
228+
}
229+
230+
function f12a(o: Thing | undefined, value: number | null) {
231+
if (o?.foo === value) {
232+
o.foo;
233+
}
234+
if (o?.["foo"] === value) {
235+
o["foo"];
236+
}
237+
if (o?.bar() === value) {
238+
o.bar;
239+
}
240+
if (o?.foo == value) {
241+
o.foo; // Error
242+
}
243+
if (o?.["foo"] == value) {
244+
o["foo"]; // Error
245+
}
246+
if (o?.bar() == value) {
208247
o.bar; // Error
209248
}
210249
}
211250

212251
function f13(o: Thing | undefined) {
213252
if (o?.foo !== undefined) {
214-
o;
215253
o.foo;
216254
}
217255
if (o?.["foo"] !== undefined) {
218-
o;
219256
o["foo"];
220257
}
221258
if (o?.bar() !== undefined) {
222-
o;
259+
o.bar;
260+
}
261+
if (o?.foo != undefined) {
262+
o.foo;
263+
}
264+
if (o?.["foo"] != undefined) {
265+
o["foo"];
266+
}
267+
if (o?.bar() != undefined) {
268+
o.bar;
269+
}
270+
}
271+
272+
function f13a(o: Thing | undefined) {
273+
if (o?.foo !== null) {
274+
o.foo; // Error
275+
}
276+
if (o?.["foo"] !== null) {
277+
o["foo"]; // Error
278+
}
279+
if (o?.bar() !== null) {
280+
o.bar; // Error
281+
}
282+
if (o?.foo != null) {
283+
o.foo;
284+
}
285+
if (o?.["foo"] != null) {
286+
o["foo"];
287+
}
288+
if (o?.bar() != null) {
223289
o.bar;
224290
}
225291
}
226292

227293
function f14(o: Thing | null) {
228294
if (o?.foo !== undefined) {
229-
o;
230295
o.foo;
231296
}
232297
if (o?.["foo"] !== undefined) {
233-
o;
234298
o["foo"];
235299
}
236300
if (o?.bar() !== undefined) {
237-
o;
238301
o.bar;
239302
}
240303
}
304+
305+
function f20(o: Thing | undefined) {
306+
if (typeof o?.foo === "number") {
307+
o.foo;
308+
}
309+
if (typeof o?.["foo"] === "number") {
310+
o["foo"];
311+
}
312+
if (typeof o?.bar() === "number") {
313+
o.bar;
314+
}
315+
if (o?.baz instanceof Error) {
316+
o.baz;
317+
}
318+
}
319+
320+
function f21(o: Thing | null) {
321+
if (typeof o?.foo === "number") {
322+
o.foo;
323+
}
324+
if (typeof o?.["foo"] === "number") {
325+
o["foo"];
326+
}
327+
if (typeof o?.bar() === "number") {
328+
o.bar;
329+
}
330+
if (o?.baz instanceof Error) {
331+
o.baz;
332+
}
333+
}

0 commit comments

Comments
 (0)