@@ -11586,7 +11586,7 @@ namespace ts {
1158611586 let types: Type[];
1158711587 const funcIsGenerator = !!func.asteriskToken;
1158811588 if (funcIsGenerator) {
11589- types = checkAndAggregateYieldOperandTypes(<Block> func.body , contextualMapper);
11589+ types = checkAndAggregateYieldOperandTypes(func, contextualMapper);
1159011590 if (types.length === 0) {
1159111591 const iterableIteratorAny = createIterableIteratorType(anyType);
1159211592 if (compilerOptions.noImplicitAny) {
@@ -11597,8 +11597,7 @@ namespace ts {
1159711597 }
1159811598 }
1159911599 else {
11600- const hasImplicitReturn = !!(func.flags & NodeFlags.HasImplicitReturn);
11601- types = checkAndAggregateReturnExpressionTypes(<Block>func.body, contextualMapper, isAsync, hasImplicitReturn);
11600+ types = checkAndAggregateReturnExpressionTypes(func, contextualMapper);
1160211601 if (!types) {
1160311602 return neverType;
1160411603 }
@@ -11656,10 +11655,10 @@ namespace ts {
1165611655 }
1165711656 }
1165811657
11659- function checkAndAggregateYieldOperandTypes(body: Block , contextualMapper? : TypeMapper): Type[] {
11658+ function checkAndAggregateYieldOperandTypes(func: FunctionLikeDeclaration , contextualMapper: TypeMapper): Type[] {
1166011659 const aggregatedTypes: Type[] = [];
1166111660
11662- forEachYieldExpression(body, yieldExpression => {
11661+ forEachYieldExpression(<Block>func. body, yieldExpression => {
1166311662 const expr = yieldExpression.expression;
1166411663 if (expr) {
1166511664 let type = checkExpressionCached(expr, contextualMapper);
@@ -11678,10 +11677,12 @@ namespace ts {
1167811677 return aggregatedTypes;
1167911678 }
1168011679
11681- function checkAndAggregateReturnExpressionTypes(body: Block, contextualMapper: TypeMapper, isAsync: boolean, hasImplicitReturn: boolean): Type[] {
11680+ function checkAndAggregateReturnExpressionTypes(func: FunctionLikeDeclaration, contextualMapper: TypeMapper): Type[] {
11681+ const isAsync = isAsyncFunctionLike(func);
1168211682 const aggregatedTypes: Type[] = [];
11683- let hasOmittedExpressions = false;
11684- forEachReturnStatement(body, returnStatement => {
11683+ let hasReturnWithNoExpression = !!(func.flags & NodeFlags.HasImplicitReturn);
11684+ let hasReturnOfTypeNever = false;
11685+ forEachReturnStatement(<Block>func.body, returnStatement => {
1168511686 const expr = returnStatement.expression;
1168611687 if (expr) {
1168711688 let type = checkExpressionCached(expr, contextualMapper);
@@ -11690,20 +11691,24 @@ namespace ts {
1169011691 // Promise/A+ compatible implementation will always assimilate any foreign promise, so the
1169111692 // return type of the body should be unwrapped to its awaited type, which should be wrapped in
1169211693 // the native Promise<T> type by the caller.
11693- type = checkAwaitedType(type, body.parent, Diagnostics.Return_expression_in_async_function_does_not_have_a_valid_callable_then_member);
11694+ type = checkAwaitedType(type, func, Diagnostics.Return_expression_in_async_function_does_not_have_a_valid_callable_then_member);
11695+ }
11696+ if (type === neverType) {
11697+ hasReturnOfTypeNever = true;
1169411698 }
11695- if (type !== neverType && !contains(aggregatedTypes, type)) {
11699+ else if (!contains(aggregatedTypes, type)) {
1169611700 aggregatedTypes.push(type);
1169711701 }
1169811702 }
1169911703 else {
11700- hasOmittedExpressions = true;
11704+ hasReturnWithNoExpression = true;
1170111705 }
1170211706 });
11703- if (aggregatedTypes.length === 0 && !hasOmittedExpressions && !hasImplicitReturn) {
11707+ if (aggregatedTypes.length === 0 && !hasReturnWithNoExpression && (hasReturnOfTypeNever ||
11708+ func.kind === SyntaxKind.FunctionExpression || func.kind === SyntaxKind.ArrowFunction)) {
1170411709 return undefined;
1170511710 }
11706- if (strictNullChecks && aggregatedTypes.length && (hasOmittedExpressions || hasImplicitReturn) ) {
11711+ if (strictNullChecks && aggregatedTypes.length && hasReturnWithNoExpression ) {
1170711712 if (!contains(aggregatedTypes, undefinedType)) {
1170811713 aggregatedTypes.push(undefinedType);
1170911714 }
0 commit comments