mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Merge pull request #7450 from Microsoft/noImplicitReturnsInAsync
unwrap promised typed in async function before doing 'noImplicitRetur…
This commit is contained in:
+12
-7
@@ -10860,11 +10860,11 @@ namespace ts {
|
||||
// If return type annotation is omitted check if function has any explicit return statements.
|
||||
// If it does not have any - its inferred return type is void - don't do any checks.
|
||||
// Otherwise get inferred return type from function body and report error only if it is not void / anytype
|
||||
const inferredReturnType = hasExplicitReturn
|
||||
? getReturnTypeOfSignature(getSignatureFromDeclaration(func))
|
||||
: voidType;
|
||||
|
||||
if (inferredReturnType === voidType || isTypeAny(inferredReturnType)) {
|
||||
if (!hasExplicitReturn) {
|
||||
return;
|
||||
}
|
||||
const inferredReturnType = getReturnTypeOfSignature(getSignatureFromDeclaration(func));
|
||||
if (isUnwrappedReturnTypeVoidOrAny(func, inferredReturnType)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -12743,7 +12743,7 @@ namespace ts {
|
||||
return checkNonThenableType(type, location, message);
|
||||
}
|
||||
else {
|
||||
if (type.id === promisedType.id || awaitedTypeStack.indexOf(promisedType.id) >= 0) {
|
||||
if (type.id === promisedType.id || indexOf(awaitedTypeStack, promisedType.id) >= 0) {
|
||||
// We have a bad actor in the form of a promise whose promised type is
|
||||
// the same promise type, or a mutually recursive promise. Return the
|
||||
// unknown type as we cannot guess the shape. If this were the actual
|
||||
@@ -13896,6 +13896,11 @@ namespace ts {
|
||||
return !!(node.kind === SyntaxKind.GetAccessor && getSetAccessorTypeAnnotationNode(<AccessorDeclaration>getDeclarationOfKind(node.symbol, SyntaxKind.SetAccessor)));
|
||||
}
|
||||
|
||||
function isUnwrappedReturnTypeVoidOrAny(func: FunctionLikeDeclaration, returnType: Type): boolean {
|
||||
const unwrappedReturnType = isAsyncFunctionLike(func) ? getPromisedType(returnType) : returnType;
|
||||
return maybeTypeOfKind(unwrappedReturnType, TypeFlags.Void | TypeFlags.Any);
|
||||
}
|
||||
|
||||
function checkReturnStatement(node: ReturnStatement) {
|
||||
// Grammar checking
|
||||
if (!checkGrammarStatementInAmbientContext(node)) {
|
||||
@@ -13944,7 +13949,7 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (compilerOptions.noImplicitReturns && !maybeTypeOfKind(returnType, TypeFlags.Void | TypeFlags.Any)) {
|
||||
else if (compilerOptions.noImplicitReturns && !isUnwrappedReturnTypeVoidOrAny(func, returnType)) {
|
||||
// The function has a return type, but the return statement doesn't have an expression.
|
||||
error(node, Diagnostics.Not_all_code_paths_return_a_value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user