@@ -468,7 +468,26 @@ namespace ts {
468
468
getRootSymbols,
469
469
getContextualType: (nodeIn: Expression, contextFlags?: ContextFlags) => {
470
470
const node = getParseTreeNode(nodeIn, isExpression);
471
- return node ? getContextualType(node, contextFlags) : undefined;
471
+ if (!node) {
472
+ return undefined;
473
+ }
474
+ const links = getNodeLinks(node);
475
+ const resolvedType = links.resolvedType;
476
+ const skipDirectInference = links.skipDirectInference;
477
+ const containingCall = findAncestor(node, isCallLikeExpression);
478
+ const containingCallResolvedSignature = containingCall && getNodeLinks(containingCall).resolvedSignature;
479
+ if (contextFlags! & ContextFlags.BaseConstraint && containingCall) {
480
+ links.resolvedType = undefined;
481
+ links.skipDirectInference = true;
482
+ getNodeLinks(containingCall).resolvedSignature = undefined;
483
+ }
484
+ const result = getContextualType(node, contextFlags);
485
+ if (contextFlags! & ContextFlags.BaseConstraint && containingCall) {
486
+ links.skipDirectInference = skipDirectInference;
487
+ links.resolvedType = resolvedType;
488
+ getNodeLinks(containingCall).resolvedSignature = containingCallResolvedSignature;
489
+ }
490
+ return result;
472
491
},
473
492
getContextualTypeForObjectLiteralElement: nodeIn => {
474
493
const node = getParseTreeNode(nodeIn, isObjectLiteralElementLike);
@@ -17675,6 +17694,14 @@ namespace ts {
17675
17694
undefined;
17676
17695
}
17677
17696
17697
+ function hasSkipDirectInferenceFlag(node: Node) {
17698
+ return !!getNodeLinks(node).skipDirectInference;
17699
+ }
17700
+
17701
+ function isFromInferenceBlockedSource(type: Type) {
17702
+ return !!(type.symbol && some(type.symbol.declarations, hasSkipDirectInferenceFlag));
17703
+ }
17704
+
17678
17705
function inferTypes(inferences: InferenceInfo[], originalSource: Type, originalTarget: Type, priority: InferencePriority = 0, contravariant = false) {
17679
17706
let symbolStack: Symbol[];
17680
17707
let visited: Map<number>;
@@ -17765,7 +17792,7 @@ namespace ts {
17765
17792
// of inference. Also, we exclude inferences for silentNeverType (which is used as a wildcard
17766
17793
// when constructing types from type parameters that had no inference candidates).
17767
17794
if (getObjectFlags(source) & ObjectFlags.NonInferrableType || source === nonInferrableAnyType || source === silentNeverType ||
17768
- (priority & InferencePriority.ReturnType && (source === autoType || source === autoArrayType))) {
17795
+ (priority & InferencePriority.ReturnType && (source === autoType || source === autoArrayType)) || isFromInferenceBlockedSource(source) ) {
17769
17796
return;
17770
17797
}
17771
17798
const inference = getInferenceInfoForType(target);
@@ -21330,34 +21357,16 @@ namespace ts {
21330
21357
}
21331
21358
21332
21359
// In a typed function call, an argument or substitution expression is contextually typed by the type of the corresponding parameter.
21333
- function getContextualTypeForArgument(callTarget: CallLikeExpression, arg: Expression, contextFlags?: ContextFlags ): Type | undefined {
21360
+ function getContextualTypeForArgument(callTarget: CallLikeExpression, arg: Expression): Type | undefined {
21334
21361
const args = getEffectiveCallArguments(callTarget);
21335
21362
const argIndex = args.indexOf(arg); // -1 for e.g. the expression of a CallExpression, or the tag of a TaggedTemplateExpression
21336
- return argIndex === -1 ? undefined : getContextualTypeForArgumentAtIndex(callTarget, argIndex, contextFlags );
21363
+ return argIndex === -1 ? undefined : getContextualTypeForArgumentAtIndex(callTarget, argIndex);
21337
21364
}
21338
21365
21339
- function getContextualTypeForArgumentAtIndex(callTarget: CallLikeExpression, argIndex: number, contextFlags?: ContextFlags ): Type {
21366
+ function getContextualTypeForArgumentAtIndex(callTarget: CallLikeExpression, argIndex: number): Type {
21340
21367
// If we're already in the process of resolving the given signature, don't resolve again as
21341
21368
// that could cause infinite recursion. Instead, return anySignature.
21342
- let signature = getNodeLinks(callTarget).resolvedSignature === resolvingSignature ? resolvingSignature : getResolvedSignature(callTarget);
21343
- if (contextFlags && contextFlags & ContextFlags.BaseConstraint && signature !== resolvingSignature && !hasTypeArguments(callTarget)) {
21344
- if (isCallOrNewExpression(callTarget) && callTarget.arguments) {
21345
- let clone = signature.omittedParameterCache && signature.omittedParameterCache[argIndex];
21346
- if (!clone) {
21347
- // clone the ast, eliding the target argument
21348
- clone = getSynthesizedClone(callTarget) as CallExpression | NewExpression;
21349
- (clone as any).id = undefined;
21350
- clone.arguments = createNodeArray([...clone.arguments!.slice(0, argIndex), createOmittedExpression(), ...clone.arguments!.slice(argIndex + 1)]);
21351
- clone.arguments![argIndex].parent = clone;
21352
- clone.parent = callTarget.parent;
21353
- (signature.omittedParameterCache || (signature.omittedParameterCache = []))[argIndex] = clone;
21354
- }
21355
- signature = getResolvedSignature(clone);
21356
- }
21357
- else if (signature.target) {
21358
- signature = getBaseSignature(signature.target);
21359
- }
21360
- }
21369
+ const signature = getNodeLinks(callTarget).resolvedSignature === resolvingSignature ? resolvingSignature : getResolvedSignature(callTarget);
21361
21370
21362
21371
if (isJsxOpeningLikeElement(callTarget) && argIndex === 0) {
21363
21372
return getEffectiveFirstArgumentForJsxSignature(signature, callTarget);
@@ -21753,7 +21762,7 @@ namespace ts {
21753
21762
}
21754
21763
/* falls through */
21755
21764
case SyntaxKind.NewExpression:
21756
- return getContextualTypeForArgument(<CallExpression | NewExpression>parent, node, contextFlags );
21765
+ return getContextualTypeForArgument(<CallExpression | NewExpression>parent, node);
21757
21766
case SyntaxKind.TypeAssertionExpression:
21758
21767
case SyntaxKind.AsExpression:
21759
21768
return isConstTypeReference((<AssertionExpression>parent).type) ? undefined : getTypeFromTypeNode((<AssertionExpression>parent).type);
@@ -21803,7 +21812,7 @@ namespace ts {
21803
21812
// (as below) instead!
21804
21813
return node.parent.contextualType;
21805
21814
}
21806
- return getContextualTypeForArgumentAtIndex(node, 0, contextFlags );
21815
+ return getContextualTypeForArgumentAtIndex(node, 0);
21807
21816
}
21808
21817
21809
21818
function getEffectiveFirstArgumentForJsxSignature(signature: Signature, node: JsxOpeningLikeElement) {
0 commit comments