Use getReturnTypeFromAnnotation instead of ad hoc checks

This commit is contained in:
Anders Hejlsberg 2018-08-06 06:51:45 -07:00
parent c367b91da7
commit dc34c5e719

View File

@ -15865,19 +15865,16 @@ namespace ts {
function getContextualReturnType(functionDecl: SignatureDeclaration): Type | undefined {
// If the containing function has a return type annotation, is a constructor, or is a get accessor whose
// corresponding set accessor has a type annotation, return statements in the function are contextually typed
if (functionDecl.kind === SyntaxKind.Constructor ||
getEffectiveReturnTypeNode(functionDecl) ||
isGetAccessorWithAnnotatedSetAccessor(functionDecl)) {
return getReturnTypeOfSignature(getSignatureFromDeclaration(functionDecl));
const returnType = getReturnTypeFromAnnotation(functionDecl);
if (returnType) {
return returnType;
}
// Otherwise, if the containing function is contextually typed by a function type with exactly one call signature
// and that call signature is non-generic, return statements are contextually typed by the return type of the signature
const signature = getContextualSignatureForFunctionLikeDeclaration(<FunctionExpression>functionDecl);
if (signature && !isResolvingReturnTypeOfSignature(signature)) {
return getReturnTypeOfSignature(signature);
}
return undefined;
}
@ -21557,9 +21554,9 @@ namespace ts {
// There is no point in doing an assignability check if the function
// has no explicit return type because the return type is directly computed
// from the yield expressions.
const returnType = getEffectiveReturnTypeNode(func);
const returnType = getReturnTypeFromAnnotation(func);
if (returnType) {
const signatureElementType = getIteratedTypeOfGenerator(getTypeFromTypeNode(returnType), isAsync) || anyType;
const signatureElementType = getIteratedTypeOfGenerator(returnType, isAsync) || anyType;
checkTypeAssignableToAndOptionallyElaborate(yieldedType, signatureElementType, node.expression || node, node.expression);
}
@ -24923,11 +24920,6 @@ namespace ts {
// TODO: Check that target label is valid
}
function isGetAccessorWithAnnotatedSetAccessor(node: SignatureDeclaration) {
return node.kind === SyntaxKind.GetAccessor
&& getEffectiveSetAccessorTypeAnnotationNode(getDeclarationOfKind<SetAccessorDeclaration>(node.symbol, SyntaxKind.SetAccessor)!) !== undefined;
}
function isUnwrappedReturnTypeVoidOrAny(func: SignatureDeclaration, returnType: Type): boolean {
const unwrappedReturnType = (getFunctionFlags(func) & FunctionFlags.AsyncGenerator) === FunctionFlags.Async
? getPromisedTypeOfPromise(returnType) // Async function