Skip to content

Commit 142aa71

Browse files
committed
fix: infer return type in implicit return arrow function
1 parent fd90fda commit 142aa71

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/resolver.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ import {
7979
isTypeOmitted,
8080
FunctionExpression,
8181
NewExpression,
82-
ArrayLiteralExpression
82+
ArrayLiteralExpression,
83+
ArrowKind,
84+
ExpressionStatement
8385
} from "./ast";
8486

8587
import {
@@ -2613,7 +2615,25 @@ export class Resolver extends DiagnosticEmitter {
26132615
/** How to proceed with eventual diagnostics. */
26142616
reportMode: ReportMode = ReportMode.REPORT
26152617
): Type | null {
2616-
return this.resolveFunctionType(node.declaration.signature, ctxFlow.actualFunction, ctxFlow.contextualTypeArguments, reportMode);
2618+
const declaration = node.declaration;
2619+
const signature = declaration.signature;
2620+
const body = declaration.body;
2621+
let functionType = this.resolveFunctionType(signature, ctxFlow.actualFunction, ctxFlow.contextualTypeArguments, reportMode);
2622+
if (
2623+
functionType &&
2624+
declaration.arrowKind != ArrowKind.NONE &&
2625+
body && body.kind == NodeKind.EXPRESSION &&
2626+
isTypeOmitted(signature.returnType)
2627+
) {
2628+
// (x) => ret, infer return type accordingt to `ret`
2629+
const expr = (<ExpressionStatement>body).expression;
2630+
const type = this.resolveExpression(expr, ctxFlow, ctxType, ReportMode.SWALLOW);
2631+
if (type) {
2632+
let signatureReference = assert(functionType.getSignature());
2633+
signatureReference.returnType = type;
2634+
}
2635+
}
2636+
return functionType;
26172637
}
26182638

26192639
// ==================================================== Elements =====================================================

0 commit comments

Comments
 (0)