@@ -1334,7 +1334,6 @@ export const enum CheckMode {
13341334 // e.g. in `const { a, ...rest } = foo`, when checking the type of `foo` to determine the type of `rest`,
13351335 // we need to preserve generic types instead of substituting them for constraints
13361336 TypeOnly = 1 << 6, // Called from getTypeOfExpression, diagnostics may be omitted
1337- ContextualSignatureReturn = 1 << 7 // Checking return expressions of functions with contextual signatures
13381337}
13391338
13401339/** @internal */
@@ -31972,6 +31971,23 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3197231971 }
3197331972 }
3197431973
31974+ function isReturnExpressionLiteralContext(node: Node) {
31975+ const ancestor = findAncestor(node, n => {
31976+ if (isCallOrNewExpression(n)) {
31977+ return "quit";
31978+ }
31979+ const parent = n.parent;
31980+ if (isStatement(parent)) {
31981+ return parent.kind === SyntaxKind.ReturnStatement || "quit";
31982+ }
31983+ if (parent.kind === SyntaxKind.ArrowFunction) {
31984+ return (parent as ArrowFunction).body === n || "quit";
31985+ }
31986+ return false;
31987+ });
31988+ return !!(ancestor && isExpression(ancestor) && getContextualTypeForReturnExpression(ancestor, /*contextFlags*/ undefined));
31989+ }
31990+
3197531991 // If the given contextual type contains instantiable types and if a mapper representing
3197631992 // return type inferences is available, instantiate those types using that mapper.
3197731993 function instantiateContextualType(contextualType: Type | undefined, node: Node, contextFlags: ContextFlags | undefined): Type | undefined {
@@ -31980,7 +31996,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3198031996 // If no inferences have been made, and none of the type parameters for which we are inferring
3198131997 // specify default types, nothing is gained from instantiating as type parameters would just be
3198231998 // replaced with their constraints similar to the apparent type.
31983- if (inferenceContext && contextFlags! & ContextFlags.ContextualSignature && some(inferenceContext.inferences, hasInferenceCandidatesOrDefault)) {
31999+ if (inferenceContext && ( contextFlags! & ContextFlags.ContextualSignature || isReturnExpressionLiteralContext(node)) && some(inferenceContext.inferences, hasInferenceCandidatesOrDefault)) {
3198432000 // For contextual signatures we incorporate all inferences made so far, e.g. from return
3198532001 // types as well as arguments to the left in a function call.
3198632002 return instantiateInstantiableTypes(contextualType, inferenceContext.nonFixingMapper);
@@ -38859,7 +38875,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3885938875 return getTypeOfSymbol(getSymbolOfDeclaration(node));
3886038876 }
3886138877
38862- function contextuallyCheckFunctionExpressionOrObjectLiteralMethod(node: FunctionExpression | ArrowFunction | MethodDeclaration, checkMode = CheckMode.Normal ) {
38878+ function contextuallyCheckFunctionExpressionOrObjectLiteralMethod(node: FunctionExpression | ArrowFunction | MethodDeclaration, checkMode?: CheckMode) {
3886338879 const links = getNodeLinks(node);
3886438880 // Check if function expression is contextually typed and assign parameter types if so.
3886538881 if (!(links.flags & NodeCheckFlags.ContextChecked)) {
@@ -38900,7 +38916,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3890038916 }
3890138917 }
3890238918 if (contextualSignature && !getReturnTypeFromAnnotation(node) && !signature.resolvedReturnType) {
38903- const returnType = getReturnTypeFromBody(node, checkMode | CheckMode.ContextualSignatureReturn );
38919+ const returnType = getReturnTypeFromBody(node, checkMode);
3890438920 if (!signature.resolvedReturnType) {
3890538921 signature.resolvedReturnType = returnType;
3890638922 }
@@ -40702,11 +40718,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4070240718 (isPropertyAssignment(parent) || isShorthandPropertyAssignment(parent) || isTemplateSpan(parent)) && isConstContext(parent.parent);
4070340719 }
4070440720
40705- function checkExpressionForMutableLocation(node: Expression, checkMode = CheckMode.Normal , forceTuple?: boolean): Type {
40721+ function checkExpressionForMutableLocation(node: Expression, checkMode?: CheckMode, forceTuple?: boolean): Type {
4070640722 const type = checkExpression(node, checkMode, forceTuple);
4070740723 return isConstContext(node) || isCommonJsExportedExpression(node) ? getRegularTypeOfLiteralType(type) :
4070840724 isTypeAssertion(node) ? type :
40709- getWidenedLiteralLikeTypeForContextualType(type, instantiateContextualType(getContextualType(node, checkMode & CheckMode.ContextualSignatureReturn ? ContextFlags.ContextualSignature : ContextFlags.None ), node, /*contextFlags*/ undefined));
40725+ getWidenedLiteralLikeTypeForContextualType(type, instantiateContextualType(getContextualType(node, /*contextFlags*/ undefined ), node, /*contextFlags*/ undefined));
4071040726 }
4071140727
4071240728 function checkPropertyAssignment(node: PropertyAssignment, checkMode?: CheckMode): Type {
0 commit comments