From dc34c5e7196ff4d4b6bfffb80e46814d444742dd Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 6 Aug 2018 06:51:45 -0700 Subject: [PATCH] Use getReturnTypeFromAnnotation instead of ad hoc checks --- src/compiler/checker.ts | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f73d4063e38..353f1f2e046 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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(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(node.symbol, SyntaxKind.SetAccessor)!) !== undefined; - } - function isUnwrappedReturnTypeVoidOrAny(func: SignatureDeclaration, returnType: Type): boolean { const unwrappedReturnType = (getFunctionFlags(func) & FunctionFlags.AsyncGenerator) === FunctionFlags.Async ? getPromisedTypeOfPromise(returnType) // Async function