diff --git a/changelog_unreleased/typescript/13860.md b/changelog_unreleased/typescript/13860.md new file mode 100644 index 000000000..5c0c8cc67 --- /dev/null +++ b/changelog_unreleased/typescript/13860.md @@ -0,0 +1,18 @@ +#### Fix union type should be printed in the multi-line variant when there are comments (#13860 by @PerfectPan) + + +```tsx +// Input +type FooBar = + | Number // this documents the first option + | void // this documents the second option + ; + +// Prettier stable +type FooBar = Number | void; // this documents the first option // this documents the second option + +// Prettier main +type FooBar = + | Number // this documents the first option + | void; // this documents the second option +``` diff --git a/src/language-js/print/type-annotation.js b/src/language-js/print/type-annotation.js index 4ec80268a..7521403e8 100644 --- a/src/language-js/print/type-annotation.js +++ b/src/language-js/print/type-annotation.js @@ -16,6 +16,7 @@ import { hasLeadingOwnLineComment, isObjectTypePropertyAFunction, shouldPrintComma, + hasComment, } from "../utils/index.js"; import { printAssignment } from "./assignment.js"; import { @@ -47,7 +48,9 @@ function shouldHugType(node) { node.type === "TSTypeReference" ); - if (node.types.length - 1 === voidCount && hasObject) { + const hasComments = node.types.some((node) => hasComment(node)); + + if (node.types.length - 1 === voidCount && hasObject && !hasComments) { return true; } } diff --git a/tests/format/typescript/union/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/union/__snapshots__/jsfmt.spec.js.snap index 47e03f90b..8f20bff37 100644 --- a/tests/format/typescript/union/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/union/__snapshots__/jsfmt.spec.js.snap @@ -47,6 +47,35 @@ interface RelayProps { } | null> | null | void, } +interface RelayProps { + articles: Array<{ + __id: string, + } | null> + | null // articles type may be null + | void, // articles type may be void +} + +type FooBar = null // null +| { /** x **/ + y: number; + z: string; +} // this documents the first option + | void // this documents the second option + ; + +type FooBarWithoutComment = null + | { + y: number; + z: string; +} + | void + ; + +type FooBar2 = + | Number // this documents the first option + | void // this documents the second option + ; + type UploadState // The upload hasnt begun yet = {type: "Not_begun"} @@ -90,6 +119,32 @@ interface RelayProps { } | null> | null | void; } +interface RelayProps { + articles: + | Array<{ + __id: string; + } | null> + | null // articles type may be null + | void; // articles type may be void +} + +type FooBar = + | null // null + | { + /** x **/ y: number; + z: string; + } // this documents the first option + | void; // this documents the second option + +type FooBarWithoutComment = null | { + y: number; + z: string; +} | void; + +type FooBar2 = + | Number // this documents the first option + | void; // this documents the second option + type UploadState = // The upload hasnt begun yet | { type: "Not_begun" } diff --git a/tests/format/typescript/union/inlining.ts b/tests/format/typescript/union/inlining.ts index 76f7dfb18..c32c9d9c5 100644 --- a/tests/format/typescript/union/inlining.ts +++ b/tests/format/typescript/union/inlining.ts @@ -7,6 +7,35 @@ interface RelayProps { } | null> | null | void, } +interface RelayProps { + articles: Array<{ + __id: string, + } | null> + | null // articles type may be null + | void, // articles type may be void +} + +type FooBar = null // null +| { /** x **/ + y: number; + z: string; +} // this documents the first option + | void // this documents the second option + ; + +type FooBarWithoutComment = null + | { + y: number; + z: string; +} + | void + ; + +type FooBar2 = + | Number // this documents the first option + | void // this documents the second option + ; + type UploadState // The upload hasnt begun yet = {type: "Not_begun"}