@@ -10,10 +10,13 @@ import {
1010 isBinaryCastExpression ,
1111 isBitwiseOperator ,
1212 isCallExpression ,
13+ isConditionalType ,
14+ isIntersectionType ,
1315 isMemberExpression ,
1416 isNullishCoalescing ,
1517 isObjectOrRecordExpression ,
1618 isObjectProperty ,
19+ isUnionType ,
1720 shouldFlatten ,
1821 startsWithNoLookaheadToken ,
1922} from "./utils/index.js" ;
@@ -531,11 +534,20 @@ function needsParens(path, options) {
531534 // fallthrough
532535 case "TSConditionalType" :
533536 case "TSConstructorType" :
534- if ( key === "extendsType" && parent . type === "TSConditionalType" ) {
535- if ( node . type === "TSConditionalType" ) {
536- return true ;
537- }
537+ case "ConditionalTypeAnnotation" :
538+ if (
539+ key === "extendsType" &&
540+ isConditionalType ( node ) &&
541+ parent . type === node . type
542+ ) {
543+ return true ;
544+ }
545+
546+ if ( key === "checkType" && isConditionalType ( parent ) ) {
547+ return true ;
548+ }
538549
550+ if ( key === "extendsType" && parent . type === "TSConditionalType" ) {
539551 let { typeAnnotation } = node . returnType || node . typeAnnotation ;
540552
541553 if (
@@ -553,15 +565,11 @@ function needsParens(path, options) {
553565 }
554566 }
555567
556- if ( key === "checkType" && parent . type === "TSConditionalType" ) {
557- return true ;
558- }
559568 // fallthrough
560569 case "TSUnionType" :
561570 case "TSIntersectionType" :
562571 if (
563- ( parent . type === "TSUnionType" ||
564- parent . type === "TSIntersectionType" ) &&
572+ ( isUnionType ( parent ) || isIntersectionType ( parent ) ) &&
565573 parent . types . length > 1 &&
566574 ( ! node . types || node . types . length > 1 )
567575 ) {
@@ -712,19 +720,6 @@ function needsParens(path, options) {
712720 ) ;
713721 }
714722
715- case "ConditionalTypeAnnotation" :
716- if (
717- key === "extendsType" &&
718- parent . type === "ConditionalTypeAnnotation" &&
719- node . type === "ConditionalTypeAnnotation"
720- ) {
721- return true ;
722- }
723-
724- if ( key === "checkType" && parent . type === "ConditionalTypeAnnotation" ) {
725- return true ;
726- }
727-
728723 // fallthrough
729724 case "OptionalIndexedAccessType" :
730725 return key === "objectType" && parent . type === "IndexedAccessType" ;
0 commit comments