Skip to content

Commit 8f45d23

Browse files
Only widen return types of function expressions in the absence of a contextual signature.
1 parent 44c5d87 commit 8f45d23

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28295,14 +28295,16 @@ namespace ts {
2829528295
returnType = getUnionType(types, UnionReduction.Subtype);
2829628296
}
2829728297

28298+
const contextualSignature = getContextualSignatureForFunctionLikeDeclaration(func);
2829828299
if (returnType || yieldType || nextType) {
28299-
if (yieldType) reportErrorsFromWidening(func, yieldType, WideningKind.GeneratorYield);
28300-
if (returnType) reportErrorsFromWidening(func, returnType, WideningKind.FunctionReturn);
28301-
if (nextType) reportErrorsFromWidening(func, nextType, WideningKind.GeneratorNext);
28300+
if (!contextualSignature) {
28301+
if (yieldType) reportErrorsFromWidening(func, yieldType, WideningKind.GeneratorYield);
28302+
if (returnType) reportErrorsFromWidening(func, returnType, WideningKind.FunctionReturn);
28303+
if (nextType) reportErrorsFromWidening(func, nextType, WideningKind.GeneratorNext);
28304+
}
2830228305
if (returnType && isUnitType(returnType) ||
2830328306
yieldType && isUnitType(yieldType) ||
2830428307
nextType && isUnitType(nextType)) {
28305-
const contextualSignature = getContextualSignatureForFunctionLikeDeclaration(func);
2830628308
const contextualType = !contextualSignature ? undefined :
2830728309
contextualSignature === getSignatureFromDeclaration(func) ? isGenerator ? undefined : returnType :
2830828310
instantiateContextualType(getReturnTypeOfSignature(contextualSignature), func);
@@ -28316,9 +28318,11 @@ namespace ts {
2831628318
}
2831728319
}
2831828320

28319-
if (yieldType) yieldType = getWidenedType(yieldType);
28320-
if (returnType) returnType = getWidenedType(returnType);
28321-
if (nextType) nextType = getWidenedType(nextType);
28321+
if (!contextualSignature) {
28322+
if (yieldType) yieldType = getWidenedType(yieldType);
28323+
if (returnType) returnType = getWidenedType(returnType);
28324+
if (nextType) nextType = getWidenedType(nextType);
28325+
}
2832228326
}
2832328327

2832428328
if (isGenerator) {

0 commit comments

Comments
 (0)