From b3da93d59c1edea05bc5940324690c92d18ac926 Mon Sep 17 00:00:00 2001 From: Henry Dineen Date: Wed, 11 Dec 2024 11:40:32 -0500 Subject: [PATCH] fix: TSArrayType printer when element is a TSUnionType --- lib/printer.ts | 3 +++ test/printer.ts | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/printer.ts b/lib/printer.ts index e3f834f0..429e0509 100644 --- a/lib/printer.ts +++ b/lib/printer.ts @@ -2122,6 +2122,9 @@ function genericPrintNoParens(path: any, options: any, print: any) { return fromString("never", options); case "TSArrayType": + if (n.elementType.type === "TSUnionType") { + return concat(["(", path.call(print, "elementType"), ")", "[]"]); + } return concat([path.call(print, "elementType"), "[]"]); case "TSLiteralType": diff --git a/test/printer.ts b/test/printer.ts index e25a961d..1638b9c6 100644 --- a/test/printer.ts +++ b/test/printer.ts @@ -2600,4 +2600,12 @@ describe("printer", function () { ), ); }); + + it("can print TSUnionType as element of TSArrayType", function () { + const node = b.tsArrayType( + b.tsUnionType([b.tsNumberKeyword(), b.tsStringKeyword()]), + ); + + assert.strictEqual(recast.print(node).code, "(number | string)[]"); + }); });