fix(isolated-declarations): wrap TSFunctionType in parentheses if it is inside the TSUnionType (#5963)

This commit is contained in:
Dunqing 2024-09-22 10:10:52 +00:00
parent eed9ac7c46
commit 6a9e71da46
3 changed files with 15 additions and 1 deletions

View file

@ -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)]);

View file

@ -30,3 +30,11 @@ function quux() {
return `${''}`
}
// Inferred type is string
function returnFunctionOrNothing() {
if (process.env.NODE_ENV === 'development') {
return
}
return () => 0;
}

View file

@ -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 ====================