diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index 11b87e2dd4f8d..ab137e29568c4 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -3032,17 +3032,28 @@ impl Gen for TSTupleType<'_> { } } +fn parenthesize_check_type_of_conditional_type(ty: &TSType<'_>) -> bool { + matches!( + ty, + TSType::TSFunctionType(_) | TSType::TSConstructorType(_) | TSType::TSConditionalType(_) + ) +} + impl Gen for TSUnionType<'_> { fn r#gen(&self, p: &mut Codegen, ctx: Context) { let Some((first, rest)) = self.types.split_first() else { return; }; - first.print(p, ctx); + p.wrap(parenthesize_check_type_of_conditional_type(first), |p| { + first.print(p, ctx); + }); for item in rest { p.print_soft_space(); p.print_str("|"); p.print_soft_space(); - item.print(p, ctx); + p.wrap(parenthesize_check_type_of_conditional_type(item), |p| { + item.print(p, ctx); + }); } } } diff --git a/crates/oxc_isolated_declarations/tests/fixtures/union-type.ts b/crates/oxc_isolated_declarations/tests/fixtures/union-type.ts new file mode 100644 index 0000000000000..3e304d83b0143 --- /dev/null +++ b/crates/oxc_isolated_declarations/tests/fixtures/union-type.ts @@ -0,0 +1,7 @@ +export class Foo { + constructor( + public one?: () => void, + protected two?: () => void, + private three?: () => void, + ) {} +} diff --git a/crates/oxc_isolated_declarations/tests/snapshots/union-type.snap b/crates/oxc_isolated_declarations/tests/snapshots/union-type.snap new file mode 100644 index 0000000000000..9b95e446e04f6 --- /dev/null +++ b/crates/oxc_isolated_declarations/tests/snapshots/union-type.snap @@ -0,0 +1,13 @@ +--- +source: crates/oxc_isolated_declarations/tests/mod.rs +input_file: crates/oxc_isolated_declarations/tests/fixtures/union-type.ts +--- +``` +==================== .D.TS ==================== + +export declare class Foo { + one?: (() => void) | undefined; + protected two?: (() => void) | undefined; + private three?; + constructor(one?: (() => void) | undefined, two?: (() => void) | undefined, three?: (() => void) | undefined); +} diff --git a/tasks/coverage/snapshots/transpile.snap b/tasks/coverage/snapshots/transpile.snap index a25cc194d81c3..8960fa9eb99ca 100644 --- a/tasks/coverage/snapshots/transpile.snap +++ b/tasks/coverage/snapshots/transpile.snap @@ -395,8 +395,8 @@ export class ClsWithRequiredInitializedParameter { //// [fnDecl.d.ts] //// type T = number[]; export declare function fnDeclBasic1(p: number[] | string[] | [T] | undefined, rParam: string): void; -export declare function fnDeclBasic2(p: (n: T) => T | undefined, rParam: string): void; -export declare function fnDeclBasic3(p: new () => any | undefined, rParam: string): void; +export declare function fnDeclBasic2(p: ((n: T) => T) | undefined, rParam: string): void; +export declare function fnDeclBasic3(p: (new () => any) | undefined, rParam: string): void; export declare function fnDeclBasic4(p: [T] | undefined, rParam: string): void; export declare function fnDeclBasic5(p: { a: T;