diff --git a/crates/oxc_isolated_declarations/src/return_type.rs b/crates/oxc_isolated_declarations/src/return_type.rs index 9af88c9f5b30f..e785245fb7fd4 100644 --- a/crates/oxc_isolated_declarations/src/return_type.rs +++ b/crates/oxc_isolated_declarations/src/return_type.rs @@ -104,8 +104,13 @@ impl<'a> FunctionReturnType<'a> { } } - // + // If there are multiple return statements, which means there must be a union with `undefined` if visitor.return_statement_count > 1 { + // Here is a union type, if the return type is a function type, we need to wrap it in parentheses + if matches!(expr_type, TSType::TSFunctionType(_)) { + expr_type = transformer.ast.ts_type_parenthesized_type(SPAN, expr_type); + } + let types = transformer .ast .vec_from_iter([expr_type, transformer.ast.ts_type_undefined_keyword(SPAN)]); diff --git a/crates/oxc_isolated_declarations/tests/fixtures/infer-return-type.ts b/crates/oxc_isolated_declarations/tests/fixtures/infer-return-type.ts index 4f0730d59b832..bf409a20e7e20 100644 --- a/crates/oxc_isolated_declarations/tests/fixtures/infer-return-type.ts +++ b/crates/oxc_isolated_declarations/tests/fixtures/infer-return-type.ts @@ -30,3 +30,11 @@ function quux() { return `${''}` } // Inferred type is string + +function returnFunctionOrNothing() { + if (process.env.NODE_ENV === 'development') { + return + } + + return () => 0; +} \ No newline at end of file diff --git a/crates/oxc_isolated_declarations/tests/snapshots/infer-return-type.snap b/crates/oxc_isolated_declarations/tests/snapshots/infer-return-type.snap index 36224eaade242..4c3bec1334d98 100644 --- a/crates/oxc_isolated_declarations/tests/snapshots/infer-return-type.snap +++ b/crates/oxc_isolated_declarations/tests/snapshots/infer-return-type.snap @@ -10,6 +10,7 @@ declare function bar(): number | undefined; declare function baz(); declare function qux(): string; declare function quux(): string; +declare function returnFunctionOrNothing(): (() => number) | undefined; ==================== Errors ====================