Skip to content

Commit d218a31

Browse files
committed
Add tests
1 parent 60b3915 commit d218a31

File tree

1 file changed

+197
-0
lines changed

1 file changed

+197
-0
lines changed

tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,60 @@ function f14(o: Thing | null) {
302302
}
303303
}
304304

305+
function f15(o: Thing | undefined, value: number) {
306+
if (o?.foo === value) {
307+
o.foo;
308+
}
309+
else {
310+
o.foo; // Error
311+
}
312+
if (o?.foo !== value) {
313+
o.foo; // Error
314+
}
315+
else {
316+
o.foo;
317+
}
318+
if (o?.foo == value) {
319+
o.foo;
320+
}
321+
else {
322+
o.foo; // Error
323+
}
324+
if (o?.foo != value) {
325+
o.foo; // Error
326+
}
327+
else {
328+
o.foo;
329+
}
330+
}
331+
332+
function f16(o: Thing | undefined) {
333+
if (o?.foo === undefined) {
334+
o.foo; // Error
335+
}
336+
else {
337+
o.foo;
338+
}
339+
if (o?.foo !== undefined) {
340+
o.foo;
341+
}
342+
else {
343+
o.foo; // Error
344+
}
345+
if (o?.foo == undefined) {
346+
o.foo; // Error
347+
}
348+
else {
349+
o.foo;
350+
}
351+
if (o?.foo != undefined) {
352+
o.foo;
353+
}
354+
else {
355+
o.foo; // Error
356+
}
357+
}
358+
305359
function f20(o: Thing | undefined) {
306360
if (typeof o?.foo === "number") {
307361
o.foo;
@@ -331,3 +385,146 @@ function f21(o: Thing | null) {
331385
o.baz;
332386
}
333387
}
388+
389+
function f22(o: Thing | undefined) {
390+
if (typeof o?.foo === "number") {
391+
o.foo;
392+
}
393+
else {
394+
o.foo; // Error
395+
}
396+
if (typeof o?.foo !== "number") {
397+
o.foo; // Error
398+
}
399+
else {
400+
o.foo;
401+
}
402+
if (typeof o?.foo == "number") {
403+
o.foo;
404+
}
405+
else {
406+
o.foo; // Error
407+
}
408+
if (typeof o?.foo != "number") {
409+
o.foo; // Error
410+
}
411+
else {
412+
o.foo;
413+
}
414+
}
415+
416+
function f23(o: Thing | undefined) {
417+
if (typeof o?.foo === "undefined") {
418+
o.foo; // Error
419+
}
420+
else {
421+
o.foo;
422+
}
423+
if (typeof o?.foo !== "undefined") {
424+
o.foo;
425+
}
426+
else {
427+
o.foo; // Error
428+
}
429+
if (typeof o?.foo == "undefined") {
430+
o.foo; // Error
431+
}
432+
else {
433+
o.foo;
434+
}
435+
if (typeof o?.foo != "undefined") {
436+
o.foo;
437+
}
438+
else {
439+
o.foo; // Error
440+
}
441+
}
442+
443+
declare function assert(x: unknown): asserts x;
444+
declare function assertNonNull<T>(x: T): asserts x is NonNullable<T>;
445+
446+
function f30(o: Thing | undefined) {
447+
if (!!true) {
448+
assert(o?.foo);
449+
o.foo;
450+
}
451+
if (!!true) {
452+
assert(o?.foo === 42);
453+
o.foo;
454+
}
455+
if (!!true) {
456+
assert(typeof o?.foo === "number");
457+
o.foo;
458+
}
459+
if (!!true) {
460+
assertNonNull(o?.foo);
461+
o.foo;
462+
}
463+
}
464+
465+
function f40(o: Thing | undefined) {
466+
switch (o?.foo) {
467+
case "abc":
468+
o.foo;
469+
break;
470+
case 42:
471+
o.foo;
472+
break;
473+
case undefined:
474+
o.foo; // Error
475+
break;
476+
default:
477+
o.foo; // Error
478+
break;
479+
}
480+
}
481+
482+
function f41(o: Thing | undefined) {
483+
switch (typeof o?.foo) {
484+
case "string":
485+
o.foo;
486+
break;
487+
case "number":
488+
o.foo;
489+
break;
490+
case "undefined":
491+
o.foo; // Error
492+
break;
493+
default:
494+
o.foo; // Error
495+
break;
496+
}
497+
}
498+
499+
// Repros from #34570
500+
501+
type Shape =
502+
| { type: 'rectangle', width: number, height: number }
503+
| { type: 'circle', radius: number }
504+
505+
function getArea(shape?: Shape) {
506+
switch (shape?.type) {
507+
case 'circle':
508+
return Math.PI * shape.radius ** 2
509+
case 'rectangle':
510+
return shape.width * shape.height
511+
default:
512+
return 0
513+
}
514+
}
515+
516+
type Feature = {
517+
id: string;
518+
geometry?: {
519+
type: string;
520+
coordinates: number[];
521+
};
522+
};
523+
524+
525+
function extractCoordinates(f: Feature): number[] {
526+
if (f.geometry?.type !== 'test') {
527+
return [];
528+
}
529+
return f.geometry.coordinates;
530+
}

0 commit comments

Comments
 (0)