@@ -31,9 +31,29 @@ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(244,9): error TS
3131tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(271,9): error TS2532: Object is possibly 'undefined'.
3232tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(274,9): error TS2532: Object is possibly 'undefined'.
3333tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(277,9): error TS2532: Object is possibly 'undefined'.
34+ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(307,9): error TS2532: Object is possibly 'undefined'.
35+ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(310,9): error TS2532: Object is possibly 'undefined'.
36+ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(319,9): error TS2532: Object is possibly 'undefined'.
37+ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(322,9): error TS2532: Object is possibly 'undefined'.
38+ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(331,9): error TS2532: Object is possibly 'undefined'.
39+ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(340,9): error TS2532: Object is possibly 'undefined'.
40+ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(343,9): error TS2532: Object is possibly 'undefined'.
41+ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(352,9): error TS2532: Object is possibly 'undefined'.
42+ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(391,9): error TS2532: Object is possibly 'undefined'.
43+ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(394,9): error TS2532: Object is possibly 'undefined'.
44+ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(403,9): error TS2532: Object is possibly 'undefined'.
45+ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(406,9): error TS2532: Object is possibly 'undefined'.
46+ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(415,9): error TS2532: Object is possibly 'undefined'.
47+ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(424,9): error TS2532: Object is possibly 'undefined'.
48+ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(427,9): error TS2532: Object is possibly 'undefined'.
49+ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(436,9): error TS2532: Object is possibly 'undefined'.
50+ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(471,13): error TS2532: Object is possibly 'undefined'.
51+ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(474,13): error TS2532: Object is possibly 'undefined'.
52+ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(488,13): error TS2532: Object is possibly 'undefined'.
53+ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(491,13): error TS2532: Object is possibly 'undefined'.
3454
3555
36- ==== tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts (33 errors) ====
56+ ==== tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts (53 errors) ====
3757 // assignments in shortcutting chain
3858 declare const o: undefined | {
3959 [key: string]: any;
@@ -401,6 +421,76 @@ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(277,9): error TS
401421 }
402422 }
403423
424+ function f15(o: Thing | undefined, value: number) {
425+ if (o?.foo === value) {
426+ o.foo;
427+ }
428+ else {
429+ o.foo; // Error
430+ ~
431+ !!! error TS2532: Object is possibly 'undefined'.
432+ }
433+ if (o?.foo !== value) {
434+ o.foo; // Error
435+ ~
436+ !!! error TS2532: Object is possibly 'undefined'.
437+ }
438+ else {
439+ o.foo;
440+ }
441+ if (o?.foo == value) {
442+ o.foo;
443+ }
444+ else {
445+ o.foo; // Error
446+ ~
447+ !!! error TS2532: Object is possibly 'undefined'.
448+ }
449+ if (o?.foo != value) {
450+ o.foo; // Error
451+ ~
452+ !!! error TS2532: Object is possibly 'undefined'.
453+ }
454+ else {
455+ o.foo;
456+ }
457+ }
458+
459+ function f16(o: Thing | undefined) {
460+ if (o?.foo === undefined) {
461+ o.foo; // Error
462+ ~
463+ !!! error TS2532: Object is possibly 'undefined'.
464+ }
465+ else {
466+ o.foo;
467+ }
468+ if (o?.foo !== undefined) {
469+ o.foo;
470+ }
471+ else {
472+ o.foo; // Error
473+ ~
474+ !!! error TS2532: Object is possibly 'undefined'.
475+ }
476+ if (o?.foo == undefined) {
477+ o.foo; // Error
478+ ~
479+ !!! error TS2532: Object is possibly 'undefined'.
480+ }
481+ else {
482+ o.foo;
483+ }
484+ if (o?.foo != undefined) {
485+ o.foo;
486+ }
487+ else {
488+ o.foo; // Error
489+ ~
490+ !!! error TS2532: Object is possibly 'undefined'.
491+ }
492+ }
493+
404494 function f20(o: Thing | undefined) {
405495 if (typeof o?.foo === "number") {
406496 o.foo;
@@ -430,4 +520,171 @@ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(277,9): error TS
430520 o.baz;
431521 }
432522 }
523+
524+ function f22(o: Thing | undefined) {
525+ if (typeof o?.foo === "number") {
526+ o.foo;
527+ }
528+ else {
529+ o.foo; // Error
530+ ~
531+ !!! error TS2532: Object is possibly 'undefined'.
532+ }
533+ if (typeof o?.foo !== "number") {
534+ o.foo; // Error
535+ ~
536+ !!! error TS2532: Object is possibly 'undefined'.
537+ }
538+ else {
539+ o.foo;
540+ }
541+ if (typeof o?.foo == "number") {
542+ o.foo;
543+ }
544+ else {
545+ o.foo; // Error
546+ ~
547+ !!! error TS2532: Object is possibly 'undefined'.
548+ }
549+ if (typeof o?.foo != "number") {
550+ o.foo; // Error
551+ ~
552+ !!! error TS2532: Object is possibly 'undefined'.
553+ }
554+ else {
555+ o.foo;
556+ }
557+ }
558+
559+ function f23(o: Thing | undefined) {
560+ if (typeof o?.foo === "undefined") {
561+ o.foo; // Error
562+ ~
563+ !!! error TS2532: Object is possibly 'undefined'.
564+ }
565+ else {
566+ o.foo;
567+ }
568+ if (typeof o?.foo !== "undefined") {
569+ o.foo;
570+ }
571+ else {
572+ o.foo; // Error
573+ ~
574+ !!! error TS2532: Object is possibly 'undefined'.
575+ }
576+ if (typeof o?.foo == "undefined") {
577+ o.foo; // Error
578+ ~
579+ !!! error TS2532: Object is possibly 'undefined'.
580+ }
581+ else {
582+ o.foo;
583+ }
584+ if (typeof o?.foo != "undefined") {
585+ o.foo;
586+ }
587+ else {
588+ o.foo; // Error
589+ ~
590+ !!! error TS2532: Object is possibly 'undefined'.
591+ }
592+ }
593+
594+ declare function assert(x: unknown): asserts x;
595+ declare function assertNonNull<T>(x: T): asserts x is NonNullable<T>;
596+
597+ function f30(o: Thing | undefined) {
598+ if (!!true) {
599+ assert(o?.foo);
600+ o.foo;
601+ }
602+ if (!!true) {
603+ assert(o?.foo === 42);
604+ o.foo;
605+ }
606+ if (!!true) {
607+ assert(typeof o?.foo === "number");
608+ o.foo;
609+ }
610+ if (!!true) {
611+ assertNonNull(o?.foo);
612+ o.foo;
613+ }
614+ }
615+
616+ function f40(o: Thing | undefined) {
617+ switch (o?.foo) {
618+ case "abc":
619+ o.foo;
620+ break;
621+ case 42:
622+ o.foo;
623+ break;
624+ case undefined:
625+ o.foo; // Error
626+ ~
627+ !!! error TS2532: Object is possibly 'undefined'.
628+ break;
629+ default:
630+ o.foo; // Error
631+ ~
632+ !!! error TS2532: Object is possibly 'undefined'.
633+ break;
634+ }
635+ }
636+
637+ function f41(o: Thing | undefined) {
638+ switch (typeof o?.foo) {
639+ case "string":
640+ o.foo;
641+ break;
642+ case "number":
643+ o.foo;
644+ break;
645+ case "undefined":
646+ o.foo; // Error
647+ ~
648+ !!! error TS2532: Object is possibly 'undefined'.
649+ break;
650+ default:
651+ o.foo; // Error
652+ ~
653+ !!! error TS2532: Object is possibly 'undefined'.
654+ break;
655+ }
656+ }
657+
658+ // Repros from #34570
659+
660+ type Shape =
661+ | { type: 'rectangle', width: number, height: number }
662+ | { type: 'circle', radius: number }
663+
664+ function getArea(shape?: Shape) {
665+ switch (shape?.type) {
666+ case 'circle':
667+ return Math.PI * shape.radius ** 2
668+ case 'rectangle':
669+ return shape.width * shape.height
670+ default:
671+ return 0
672+ }
673+ }
674+
675+ type Feature = {
676+ id: string;
677+ geometry?: {
678+ type: string;
679+ coordinates: number[];
680+ };
681+ };
682+
683+
684+ function extractCoordinates(f: Feature): number[] {
685+ if (f.geometry?.type !== 'test') {
686+ return [];
687+ }
688+ return f.geometry.coordinates;
689+ }
433690
0 commit comments