MCPcopy Index your code
hub / github.com/nodejs/node / inferTypeArguments

Function inferTypeArguments

test/fixtures/snapshot/typescript.js:75470–75541  ·  view source on GitHub ↗
(node, signature, args, checkMode, context)

Source from the content-addressed store, hash-verified

75468 thisArgumentType;
75469 }
75470 function inferTypeArguments(node, signature, args, checkMode, context) {
75471 if (ts.isJsxOpeningLikeElement(node)) {
75472 return inferJsxTypeArguments(node, signature, checkMode, context);
75473 }
75474 // If a contextual type is available, infer from that type to the return type of the call expression. For
75475 // example, given a 'function wrap<T, U>(cb: (x: T) => U): (x: T) => U' and a call expression
75476 // 'let f: (x: string) => number = wrap(s => s.length)', we infer from the declared type of 'f' to the
75477 // return type of 'wrap'.
75478 if (node.kind !== 165 /* SyntaxKind.Decorator */) {
75479 var contextualType = getContextualType(node, ts.every(signature.typeParameters, function (p) { return !!getDefaultFromTypeParameter(p); }) ? 8 /* ContextFlags.SkipBindingPatterns */ : 0 /* ContextFlags.None */);
75480 if (contextualType) {
75481 var inferenceTargetType = getReturnTypeOfSignature(signature);
75482 if (couldContainTypeVariables(inferenceTargetType)) {
75483 // We clone the inference context to avoid disturbing a resolution in progress for an
75484 // outer call expression. Effectively we just want a snapshot of whatever has been
75485 // inferred for any outer call expression so far.
75486 var outerContext = getInferenceContext(node);
75487 var outerMapper = getMapperFromContext(cloneInferenceContext(outerContext, 1 /* InferenceFlags.NoDefault */));
75488 var instantiatedType = instantiateType(contextualType, outerMapper);
75489 // If the contextual type is a generic function type with a single call signature, we
75490 // instantiate the type with its own type parameters and type arguments. This ensures that
75491 // the type parameters are not erased to type any during type inference such that they can
75492 // be inferred as actual types from the contextual type. For example:
75493 // declare function arrayMap<T, U>(f: (x: T) => U): (a: T[]) => U[];
75494 // const boxElements: <A>(a: A[]) => { value: A }[] = arrayMap(value => ({ value }));
75495 // Above, the type of the 'value' parameter is inferred to be 'A'.
75496 var contextualSignature = getSingleCallSignature(instantiatedType);
75497 var inferenceSourceType = contextualSignature && contextualSignature.typeParameters ?
75498 getOrCreateTypeFromSignature(getSignatureInstantiationWithoutFillingInTypeArguments(contextualSignature, contextualSignature.typeParameters)) :
75499 instantiatedType;
75500 // Inferences made from return types have lower priority than all other inferences.
75501 inferTypes(context.inferences, inferenceSourceType, inferenceTargetType, 128 /* InferencePriority.ReturnType */);
75502 // Create a type mapper for instantiating generic contextual types using the inferences made
75503 // from the return type. We need a separate inference pass here because (a) instantiation of
75504 // the source type uses the outer context's return mapper (which excludes inferences made from
75505 // outer arguments), and (b) we don't want any further inferences going into this context.
75506 var returnContext = createInferenceContext(signature.typeParameters, signature, context.flags);
75507 var returnSourceType = instantiateType(contextualType, outerContext && outerContext.returnMapper);
75508 inferTypes(returnContext.inferences, returnSourceType, inferenceTargetType);
75509 context.returnMapper = ts.some(returnContext.inferences, hasInferenceCandidates) ? getMapperFromContext(cloneInferredPartOfContext(returnContext)) : undefined;
75510 }
75511 }
75512 }
75513 var restType = getNonArrayRestType(signature);
75514 var argCount = restType ? Math.min(getParameterCount(signature) - 1, args.length) : args.length;
75515 if (restType && restType.flags & 262144 /* TypeFlags.TypeParameter */) {
75516 var info = ts.find(context.inferences, function (info) { return info.typeParameter === restType; });
75517 if (info) {
75518 info.impliedArity = ts.findIndex(args, isSpreadArgument, argCount) < 0 ? args.length - argCount : undefined;
75519 }
75520 }
75521 var thisType = getThisTypeOfSignature(signature);
75522 if (thisType && couldContainTypeVariables(thisType)) {
75523 var thisArgumentNode = getThisArgumentOfCall(node);
75524 inferTypes(context.inferences, getThisArgumentType(thisArgumentNode), thisType);
75525 }
75526 for (var i = 0; i < argCount; i++) {
75527 var arg = args[i];

Callers 2

chooseOverloadFunction · 0.85

Calls 15

inferJsxTypeArgumentsFunction · 0.85
getContextualTypeFunction · 0.85
getReturnTypeOfSignatureFunction · 0.85
getInferenceContextFunction · 0.85
getMapperFromContextFunction · 0.85
cloneInferenceContextFunction · 0.85
instantiateTypeFunction · 0.85
getSingleCallSignatureFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…