Skip to content

Enhance FunctionExpression type inference #2458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2683,9 +2683,19 @@ export class Resolver extends DiagnosticEmitter {
) {
// (x) => ret, infer return type accordingt to `ret`
const expr = (<ExpressionStatement>body).expression;
const type = this.resolveExpression(expr, ctxFlow, ctxType, reportMode);
let signatureReference = assert(functionType.getSignature());
// create a temp flow to resolve expression
let tempFlow = Flow.createParent(ctxFlow.actualFunction);
let parameters = signature.parameters;
// return type of resolveFunctionType should have same parameter length with signature
assert(signatureReference.parameterTypes.length == parameters.length);
for (let i = 0, k = parameters.length; i < k; i++) {
const parameter = parameters[i];
const type = signatureReference.parameterTypes[i];
tempFlow.addScopedDummyLocal(parameter.name.text, type, parameter);
}
const type = this.resolveExpression(expr, tempFlow, ctxType, reportMode);
if (type) {
let signatureReference = assert(functionType.getSignature());
signatureReference.returnType = type;
}
}
Expand Down
Loading