@@ -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" ;
@@ -525,11 +528,20 @@ function needsParens(path, options) {
525528 // fallthrough
526529 case "TSConditionalType" :
527530 case "TSConstructorType" :
528- if ( key === "extendsType" && parent . type === "TSConditionalType" ) {
529- if ( node . type === "TSConditionalType" ) {
530- return true ;
531- }
531+ case "ConditionalTypeAnnotation" :
532+ if (
533+ key === "extendsType" &&
534+ isConditionalType ( node ) &&
535+ parent . type === node . type
536+ ) {
537+ return true ;
538+ }
539+
540+ if ( key === "checkType" && isConditionalType ( parent ) ) {
541+ return true ;
542+ }
532543
544+ if ( key === "extendsType" && parent . type === "TSConditionalType" ) {
533545 let { typeAnnotation } = node . returnType || node . typeAnnotation ;
534546
535547 if (
@@ -547,15 +559,11 @@ function needsParens(path, options) {
547559 }
548560 }
549561
550- if ( key === "checkType" && parent . type === "TSConditionalType" ) {
551- return true ;
552- }
553562 // fallthrough
554563 case "TSUnionType" :
555564 case "TSIntersectionType" :
556565 if (
557- ( parent . type === "TSUnionType" ||
558- parent . type === "TSIntersectionType" ) &&
566+ ( isUnionType ( parent ) || isIntersectionType ( parent ) ) &&
559567 parent . types . length > 1 &&
560568 ( ! node . types || node . types . length > 1 )
561569 ) {
@@ -706,19 +714,6 @@ function needsParens(path, options) {
706714 ) ;
707715 }
708716
709- case "ConditionalTypeAnnotation" :
710- if (
711- key === "extendsType" &&
712- parent . type === "ConditionalTypeAnnotation" &&
713- node . type === "ConditionalTypeAnnotation"
714- ) {
715- return true ;
716- }
717-
718- if ( key === "checkType" && parent . type === "ConditionalTypeAnnotation" ) {
719- return true ;
720- }
721-
722717 // fallthrough
723718 case "OptionalIndexedAccessType" :
724719 return key === "objectType" && parent . type === "IndexedAccessType" ;
0 commit comments