diff --git a/src/resolver.ts b/src/resolver.ts index 46d5d16570..523d1d8d44 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -79,7 +79,9 @@ import { isTypeOmitted, FunctionExpression, NewExpression, - ArrayLiteralExpression + ArrayLiteralExpression, + ArrowKind, + ExpressionStatement } from "./ast"; import { @@ -2613,7 +2615,25 @@ export class Resolver extends DiagnosticEmitter { /** How to proceed with eventual diagnostics. */ reportMode: ReportMode = ReportMode.REPORT ): Type | null { - return this.resolveFunctionType(node.declaration.signature, ctxFlow.actualFunction, ctxFlow.contextualTypeArguments, reportMode); + const declaration = node.declaration; + const signature = declaration.signature; + const body = declaration.body; + let functionType = this.resolveFunctionType(signature, ctxFlow.actualFunction, ctxFlow.contextualTypeArguments, reportMode); + if ( + functionType && + declaration.arrowKind != ArrowKind.NONE && + body && body.kind == NodeKind.EXPRESSION && + isTypeOmitted(signature.returnType) + ) { + // (x) => ret, infer return type accordingt to `ret` + const expr = (body).expression; + const type = this.resolveExpression(expr, ctxFlow, ctxType, reportMode); + if (type) { + let signatureReference = assert(functionType.getSignature()); + signatureReference.returnType = type; + } + } + return functionType; } // ==================================================== Elements ===================================================== diff --git a/tests/compiler/std/array.ts b/tests/compiler/std/array.ts index 50a937347c..46b4f5d1ce 100644 --- a/tests/compiler/std/array.ts +++ b/tests/compiler/std/array.ts @@ -780,7 +780,7 @@ var i: i32; // Array#map /////////////////////////////////////////////////////////////////////////////////////// { - let newArr = arr.map((value: i32) => value); + let newArr = arr.map((value: i32, index: i32, arr: Array) => value); assert(newArr.length == 4); assert(newArr[0] == arr[0]);