Treat 'return' as 'return undefined' for type checking purposes

This commit is contained in:
Anders Hejlsberg
2016-03-05 15:23:00 -08:00
parent 8db7af035d
commit d0e4b4ae35
+9 -7
View File
@@ -14135,12 +14135,12 @@ namespace ts {
}
}
if (node.expression) {
if (strictNullChecks || node.expression) {
const func = getContainingFunction(node);
if (func) {
const signature = getSignatureFromDeclaration(func);
const returnType = getReturnTypeOfSignature(signature);
const exprType = checkExpressionCached(node.expression);
const exprType = node.expression ? checkExpressionCached(node.expression) : undefinedType;
if (func.asteriskToken) {
// A generator does not need its return expressions checked against its return type.
@@ -14151,26 +14151,28 @@ namespace ts {
}
if (func.kind === SyntaxKind.SetAccessor) {
error(node.expression, Diagnostics.Setters_cannot_return_a_value);
if (node.expression) {
error(node.expression, Diagnostics.Setters_cannot_return_a_value);
}
}
else if (func.kind === SyntaxKind.Constructor) {
if (!checkTypeAssignableTo(exprType, returnType, node.expression)) {
if (node.expression && !checkTypeAssignableTo(exprType, returnType, node.expression)) {
error(node.expression, Diagnostics.Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class);
}
}
else if (func.type || isGetAccessorWithAnnotatedSetAccessor(func)) {
if (isAsyncFunctionLike(func)) {
const promisedType = getPromisedType(returnType);
const awaitedType = checkAwaitedType(exprType, node.expression, Diagnostics.Return_expression_in_async_function_does_not_have_a_valid_callable_then_member);
const awaitedType = checkAwaitedType(exprType, node.expression || node, Diagnostics.Return_expression_in_async_function_does_not_have_a_valid_callable_then_member);
if (promisedType) {
// If the function has a return type, but promisedType is
// undefined, an error will be reported in checkAsyncFunctionReturnType
// so we don't need to report one here.
checkTypeAssignableTo(awaitedType, promisedType, node.expression);
checkTypeAssignableTo(awaitedType, promisedType, node.expression || node);
}
}
else {
checkTypeAssignableTo(exprType, returnType, node.expression);
checkTypeAssignableTo(exprType, returnType, node.expression || node);
}
}
}