Skip to content

Commit b3bebe3

Browse files
Copilotjakebailey
andcommitted
Fix isolated declarations to not flag function expressions in call arguments
Function expressions and arrow functions used as arguments to function calls were incorrectly being flagged for needing explicit return type annotations with --isolatedDeclarations. This change modifies the logic in expressionToTypeNode.ts to skip inference fallback reporting for function expressions that are direct arguments to call expressions. The fix addresses the underlying issue by checking if the function expression's parent is a CallExpression and passing reportFallback=false in those cases, preventing the inference fallback error from being generated in the first place. Fixes #62085 Co-authored-by: jakebailey <[email protected]>
1 parent f163c16 commit b3bebe3

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

src/compiler/expressionToTypeNode.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,9 @@ export function createSyntacticTypeNodeBuilder(
731731
case SyntaxKind.ConstructorType:
732732
case SyntaxKind.FunctionExpression:
733733
case SyntaxKind.ArrowFunction:
734+
// Don't report inference fallback for function expressions that are arguments to call expressions
735+
const shouldReportFallback = !(node.kind === SyntaxKind.FunctionExpression || node.kind === SyntaxKind.ArrowFunction) || !isCallExpression(node.parent);
736+
return createReturnFromSignature(node, symbol, context, shouldReportFallback);
734737
case SyntaxKind.JSDocFunctionType:
735738
case SyntaxKind.JSDocSignature:
736739
return createReturnFromSignature(node, symbol, context);
@@ -968,7 +971,9 @@ export function createSyntacticTypeNodeBuilder(
968971
return failed;
969972
}
970973
function typeFromFunctionLikeExpression(fnNode: FunctionExpression | ArrowFunction, context: SyntacticTypeNodeBuilderContext) {
971-
const returnType = createReturnFromSignature(fnNode, /*symbol*/ undefined, context);
974+
// Don't report inference fallback for function expressions that are arguments to call expressions
975+
const shouldReportFallback = !isCallExpression(fnNode.parent);
976+
const returnType = createReturnFromSignature(fnNode, /*symbol*/ undefined, context, shouldReportFallback);
972977
const typeParameters = reuseTypeParameters(fnNode.typeParameters, context);
973978
const parameters = fnNode.parameters.map(p => ensureParameter(p, context));
974979
return syntacticResult(

src/compiler/transformers/declarations.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ import {
9494
isBinaryExpression,
9595
isBindingElement,
9696
isBindingPattern,
97-
isCallExpression,
9897
isClassDeclaration,
9998
isClassElement,
10099
isComputedPropertyName,
@@ -332,13 +331,6 @@ export function transformDeclarations(context: TransformationContext): Transform
332331
reportExpandoFunctionErrors(node);
333332
}
334333
else {
335-
// Don't report errors for function expressions that are arguments to call expressions
336-
if (
337-
(node.kind === SyntaxKind.FunctionExpression || node.kind === SyntaxKind.ArrowFunction) &&
338-
isCallExpression(node.parent)
339-
) {
340-
return;
341-
}
342334
context.addDiagnostic(getIsolatedDeclarationError(node));
343335
}
344336
}

0 commit comments

Comments
 (0)