@@ -27160,6 +27160,16 @@ namespace ts {
2716027160 return getInferredTypes(context);
2716127161 }
2716227162
27163+ function getThisArgumentType(thisArgumentNode: LeftHandSideExpression | undefined) {
27164+ if (!thisArgumentNode) {
27165+ return voidType;
27166+ }
27167+ const thisArgumentType = checkExpression(thisArgumentNode);
27168+ return isOptionalChainRoot(thisArgumentNode.parent) ? getNonNullableType(thisArgumentType) :
27169+ isOptionalChain(thisArgumentNode.parent) ? removeOptionalTypeMarker(thisArgumentType) :
27170+ thisArgumentType;
27171+ }
27172+
2716327173 function inferTypeArguments(node: CallLikeExpression, signature: Signature, args: readonly Expression[], checkMode: CheckMode, context: InferenceContext): Type[] {
2716427174 if (isJsxOpeningLikeElement(node)) {
2716527175 return inferJsxTypeArguments(node, signature, checkMode, context);
@@ -27215,8 +27225,7 @@ namespace ts {
2721527225 const thisType = getThisTypeOfSignature(signature);
2721627226 if (thisType) {
2721727227 const thisArgumentNode = getThisArgumentOfCall(node);
27218- const thisArgumentType = thisArgumentNode ? checkExpression(thisArgumentNode) : voidType;
27219- inferTypes(context.inferences, thisArgumentType, thisType);
27228+ inferTypes(context.inferences, getThisArgumentType(thisArgumentNode), thisType);
2722027229 }
2722127230
2722227231 for (let i = 0; i < argCount; i++) {
@@ -27457,20 +27466,7 @@ namespace ts {
2745727466 // If the signature's 'this' type is voidType, then the check is skipped -- anything is compatible.
2745827467 // If the expression is a new expression, then the check is skipped.
2745927468 const thisArgumentNode = getThisArgumentOfCall(node);
27460- let thisArgumentType: Type;
27461- if (thisArgumentNode) {
27462- thisArgumentType = checkExpression(thisArgumentNode);
27463- if (isOptionalChainRoot(thisArgumentNode.parent)) {
27464- thisArgumentType = getNonNullableType(thisArgumentType);
27465- }
27466- else if (isOptionalChain(thisArgumentNode.parent)) {
27467- thisArgumentType = removeOptionalTypeMarker(thisArgumentType);
27468- }
27469- }
27470- else {
27471- thisArgumentType = voidType;
27472- }
27473-
27469+ const thisArgumentType = getThisArgumentType(thisArgumentNode);
2747427470 const errorNode = reportErrors ? (thisArgumentNode || node) : undefined;
2747527471 const headMessage = Diagnostics.The_this_context_of_type_0_is_not_assignable_to_method_s_this_of_type_1;
2747627472 if (!checkTypeRelatedTo(thisArgumentType, thisType, relation, errorNode, headMessage, containingMessageChain, errorOutputContainer)) {
0 commit comments