Skip to content

Commit f4b2ec7

Browse files
committed
Refactor conditional logic to avoid fallthrough and to make the control flow more clear
1 parent dd38c4d commit f4b2ec7

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26681,7 +26681,7 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
2668126681
function getContextualTypeForReturnExpression(node: Expression): Type | undefined {
2668226682
const func = getContainingFunction(node);
2668326683
if (func) {
26684-
let contextualReturnType = getContextualReturnType(func);
26684+
const contextualReturnType = getContextualReturnType(func);
2668526685
if (contextualReturnType) {
2668626686
const functionFlags = getFunctionFlags(func);
2668726687
if (functionFlags & FunctionFlags.Generator) { // Generator or AsyncGenerator function
@@ -26690,12 +26690,17 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
2669026690
if (!iterationTypes) {
2669126691
return undefined;
2669226692
}
26693-
contextualReturnType = iterationTypes.returnType;
26693+
if (functionFlags & FunctionFlags.Async) {
26694+
// unwrap Promise to get the awaited type without the `Awaited<T>` alias
26695+
const contextualAwaitedType = mapType(iterationTypes.returnType, getAwaitedTypeNoAlias);
26696+
return contextualAwaitedType && getUnionType([contextualAwaitedType, createPromiseLikeType(contextualAwaitedType)]);
26697+
}
26698+
return iterationTypes.returnType;
2669426699
}
2669526700

26696-
if (functionFlags & FunctionFlags.Async) { // Async function or AsyncGenerator function
26697-
const contextualAwaitedType = mapType(contextualReturnType, functionFlags & FunctionFlags.Generator ? getAwaitedTypeNoAlias : getAwaitedTypeOfPromise);
26698-
return contextualAwaitedType && getUnionType([contextualAwaitedType, createPromiseLikeType(contextualAwaitedType)]);
26701+
if (functionFlags & FunctionFlags.Async) {
26702+
const contextualTypeOfPromise = mapType(contextualReturnType, getAwaitedTypeOfPromise);
26703+
return contextualTypeOfPromise && getUnionType([contextualTypeOfPromise, createPromiseLikeType(contextualTypeOfPromise)]);
2669926704
}
2670026705

2670126706
return contextualReturnType; // Regular function or Generator function

0 commit comments

Comments
 (0)