Normalize union type comment alignment in TypeScript conditional types (#18644)

Co-authored-by: fisker <lionkay@gmail.com>
This commit is contained in:
Lindsay Glennandfisker authored and GitHub committed 2026-01-15 20:31:27 +08:00
1 parent 7f455be5af
commit 72a182f594
4 files changed
+231 -1

No files matched your search

+25
View File
@@ -0,0 +1,25 @@
#### Fix conditional type comment indentation (#18644 by @lindsaycode05)
<!-- prettier-ignore -->
```tsx
// Input
type T = any extends B
// Comment
// Multiline comment
? undefined | NonNullable<B>[foo]
: B[foo];
// Prettier stable
type T = any extends B
? // Comment
// Multiline comment
undefined | NonNullable<B>[foo]
: B[foo];
// Prettier main
type T = any extends B
? // Comment
// Multiline comment
undefined | NonNullable<B>[foo]
: B[foo];
```
+5 -1
View File
@@ -43,7 +43,6 @@ function printUnionType(path, options, print) {
// If there's a leading comment, the parent is doing the indentation // If there's a leading comment, the parent is doing the indentation
const shouldIndent = const shouldIndent =
parent.type !== "TypeParameterInstantiation" && parent.type !== "TypeParameterInstantiation" &&
(!isConditionalType(parent) || !options.experimentalTernaries) &&
parent.type !== "TSTypeParameterInstantiation" && parent.type !== "TSTypeParameterInstantiation" &&
parent.type !== "GenericTypeAnnotation" && parent.type !== "GenericTypeAnnotation" &&
parent.type !== "TSTypeReference" && parent.type !== "TSTypeReference" &&
@@ -54,6 +53,11 @@ function printUnionType(path, options, print) {
!parent.name && !parent.name &&
path.grandparent.this !== parent path.grandparent.this !== parent
) && ) &&
!(
isConditionalType(parent) &&
!options.experimentalTernaries &&
hasLeadingOwnLineComment(options.originalText, node)
) &&
!( !(
(isTypeAlias(parent) || parent.type === "VariableDeclarator") && (isTypeAlias(parent) || parent.type === "VariableDeclarator") &&
hasLeadingOwnLineComment(options.originalText, node) hasLeadingOwnLineComment(options.originalText, node)
@@ -79,6 +79,47 @@ type T = test extends B
type T = test extends B ? test extends B /* c type T = test extends B ? test extends B /* c
c */? foo : bar : bar; c */? foo : bar : bar;
type T = any extends B
// Comment
// Multiline comment
? B | C
: D;
T = any instanceof B
// Comment
// Multiline comment
? B | C
: D;
type T = any extends B
// Comment
? B | C
: D;
T = any instanceof B
// Comment
? B | C
: D;
type T = any extends B
/**
* Comment
*/
? B | C
: D;
T = any instanceof B
/**
* Comment
*/
? B | C
: D;
type T = any extends B
?/* Comment */
B | C
: D;
T = any instanceof B
?/* Comment */
B | C
: D;
=====================================output===================================== =====================================output=====================================
type A = type A =
B extends T ? B extends T ?
@@ -158,6 +199,46 @@ c */
: bar : bar
: bar; : bar;
type T =
any extends B ?
// Comment
// Multiline comment
B | C
: D;
T =
any instanceof B ?
// Comment
// Multiline comment
B | C
: D;
type T =
any extends B ?
// Comment
B | C
: D;
T =
any instanceof B ?
// Comment
B | C
: D;
type T =
any extends B ?
/**
* Comment
*/
B | C
: D;
T = any instanceof B ?
/**
* Comment
*/
B | C
: D;
type T = any extends B ? /* Comment */ B | C : D;
T = any instanceof B ? /* Comment */ B | C : D;
================================================================================ ================================================================================
`; `;
@@ -239,6 +320,47 @@ type T = test extends B
type T = test extends B ? test extends B /* c type T = test extends B ? test extends B /* c
c */? foo : bar : bar; c */? foo : bar : bar;
type T = any extends B
// Comment
// Multiline comment
? B | C
: D;
T = any instanceof B
// Comment
// Multiline comment
? B | C
: D;
type T = any extends B
// Comment
? B | C
: D;
T = any instanceof B
// Comment
? B | C
: D;
type T = any extends B
/**
* Comment
*/
? B | C
: D;
T = any instanceof B
/**
* Comment
*/
? B | C
: D;
type T = any extends B
?/* Comment */
B | C
: D;
T = any instanceof B
?/* Comment */
B | C
: D;
=====================================output===================================== =====================================output=====================================
type A = B extends T type A = B extends T
? // comment ? // comment
@@ -310,6 +432,44 @@ c */
: bar : bar
: bar; : bar;
type T = any extends B
? // Comment
// Multiline comment
B | C
: D;
T =
any instanceof B
? // Comment
// Multiline comment
B | C
: D;
type T = any extends B
? // Comment
B | C
: D;
T =
any instanceof B
? // Comment
B | C
: D;
type T = any extends B
? /**
* Comment
*/
B | C
: D;
T =
any instanceof B
? /**
* Comment
*/
B | C
: D;
type T = any extends B ? /* Comment */ B | C : D;
T = any instanceof B ? /* Comment */ B | C : D;
================================================================================ ================================================================================
`; `;
@@ -69,3 +69,44 @@ type T = test extends B
type T = test extends B ? test extends B /* c type T = test extends B ? test extends B /* c
c */? foo : bar : bar; c */? foo : bar : bar;
type T = any extends B
// Comment
// Multiline comment
? B | C
: D;
T = any instanceof B
// Comment
// Multiline comment
? B | C
: D;
type T = any extends B
// Comment
? B | C
: D;
T = any instanceof B
// Comment
? B | C
: D;
type T = any extends B
/**
* Comment
*/
? B | C
: D;
T = any instanceof B
/**
* Comment
*/
? B | C
: D;
type T = any extends B
?/* Comment */
B | C
: D;
T = any instanceof B
?/* Comment */
B | C
: D;