Skip to content

Commit

Permalink
Refactor conditional logic to avoid fallthrough and to make the contr…
Browse files Browse the repository at this point in the history
…ol flow more clear
  • Loading branch information
Andarist committed Jan 31, 2022
1 parent 9d55f13 commit 0ea6851
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26033,7 +26033,7 @@ namespace ts {
function getContextualTypeForReturnExpression(node: Expression): Type | undefined {
const func = getContainingFunction(node);
if (func) {
let contextualReturnType = getContextualReturnType(func);
const contextualReturnType = getContextualReturnType(func);
if (contextualReturnType) {
const functionFlags = getFunctionFlags(func);
if (functionFlags & FunctionFlags.Generator) { // Generator or AsyncGenerator function
Expand All @@ -26042,12 +26042,17 @@ namespace ts {
if (!iterationTypes) {
return undefined;
}
contextualReturnType = iterationTypes.returnType;
if (functionFlags & FunctionFlags.Async) {
// unwrap Promise to get the awaited type without the `Awaited<T>` alias
const contextualAwaitedType = mapType(iterationTypes.returnType, getAwaitedTypeNoAlias);
return contextualAwaitedType && getUnionType([contextualAwaitedType, createPromiseLikeType(contextualAwaitedType)]);
}
return iterationTypes.returnType;
}

if (functionFlags & FunctionFlags.Async) { // Async function or AsyncGenerator function
const contextualAwaitedType = mapType(contextualReturnType, functionFlags & FunctionFlags.Generator ? getAwaitedTypeNoAlias : getAwaitedTypeOfPromise);
return contextualAwaitedType && getUnionType([contextualAwaitedType, createPromiseLikeType(contextualAwaitedType)]);
if (functionFlags & FunctionFlags.Async) {
const contextualTypeOfPromise = mapType(contextualReturnType, getAwaitedTypeOfPromise);
return contextualTypeOfPromise && getUnionType([contextualTypeOfPromise, createPromiseLikeType(contextualTypeOfPromise)]);
}

return contextualReturnType; // Regular function or Generator function
Expand Down

0 comments on commit 0ea6851

Please sign in to comment.