From 72a182f5940f95d595453b741cee1a3093be8504 Mon Sep 17 00:00:00 2001 From: Lindsay Glenn <88434441+lindsaycode05@users.noreply.github.com> Date: Thu, 15 Jan 2026 14:31:27 +0200 Subject: [PATCH] Normalize union type comment alignment in TypeScript conditional types (#18644) Co-authored-by: fisker --- changelog_unreleased/typescript/18644.md | 25 +++ src/language-js/print/union-type.js | 6 +- .../__snapshots__/format.test.js.snap | 160 ++++++++++++++++++ .../typescript/conditional-types/comments.ts | 41 +++++ 4 files changed, 231 insertions(+), 1 deletion(-) create mode 100644 changelog_unreleased/typescript/18644.md diff --git a/changelog_unreleased/typescript/18644.md b/changelog_unreleased/typescript/18644.md new file mode 100644 index 000000000..f203c1092 --- /dev/null +++ b/changelog_unreleased/typescript/18644.md @@ -0,0 +1,25 @@ +#### Fix conditional type comment indentation (#18644 by @lindsaycode05) + + +```tsx +// Input +type T = any extends B + // Comment + // Multiline comment + ? undefined | NonNullable[foo] + : B[foo]; + +// Prettier stable +type T = any extends B + ? // Comment + // Multiline comment + undefined | NonNullable[foo] + : B[foo]; + +// Prettier main +type T = any extends B + ? // Comment + // Multiline comment + undefined | NonNullable[foo] + : B[foo]; +``` diff --git a/src/language-js/print/union-type.js b/src/language-js/print/union-type.js index ef54974eb..85a230e61 100644 --- a/src/language-js/print/union-type.js +++ b/src/language-js/print/union-type.js @@ -43,7 +43,6 @@ function printUnionType(path, options, print) { // If there's a leading comment, the parent is doing the indentation const shouldIndent = parent.type !== "TypeParameterInstantiation" && - (!isConditionalType(parent) || !options.experimentalTernaries) && parent.type !== "TSTypeParameterInstantiation" && parent.type !== "GenericTypeAnnotation" && parent.type !== "TSTypeReference" && @@ -54,6 +53,11 @@ function printUnionType(path, options, print) { !parent.name && path.grandparent.this !== parent ) && + !( + isConditionalType(parent) && + !options.experimentalTernaries && + hasLeadingOwnLineComment(options.originalText, node) + ) && !( (isTypeAlias(parent) || parent.type === "VariableDeclarator") && hasLeadingOwnLineComment(options.originalText, node) diff --git a/tests/format/typescript/conditional-types/__snapshots__/format.test.js.snap b/tests/format/typescript/conditional-types/__snapshots__/format.test.js.snap index 9dd0ec4ad..4a47d7eb7 100644 --- a/tests/format/typescript/conditional-types/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/conditional-types/__snapshots__/format.test.js.snap @@ -79,6 +79,47 @@ type T = test extends B type T = test extends B ? test extends B /* c 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===================================== type A = B extends T ? @@ -158,6 +199,46 @@ c */ : 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 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===================================== type A = B extends T ? // comment @@ -310,6 +432,44 @@ c */ : 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; + ================================================================================ `; diff --git a/tests/format/typescript/conditional-types/comments.ts b/tests/format/typescript/conditional-types/comments.ts index bcbe41939..9f322e103 100644 --- a/tests/format/typescript/conditional-types/comments.ts +++ b/tests/format/typescript/conditional-types/comments.ts @@ -69,3 +69,44 @@ type T = test extends B type T = test extends B ? test extends B /* c 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;