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

Function inferToMappedType

test/fixtures/snapshot/typescript.js:68857–68905  ·  view source on GitHub ↗
(source, target, constraintType)

Source from the content-addressed store, hash-verified

68855 }
68856 }
68857 function inferToMappedType(source, target, constraintType) {
68858 if (constraintType.flags & 1048576 /* TypeFlags.Union */) {
68859 var result = false;
68860 for (var _i = 0, _a = constraintType.types; _i < _a.length; _i++) {
68861 var type = _a[_i];
68862 result = inferToMappedType(source, target, type) || result;
68863 }
68864 return result;
68865 }
68866 if (constraintType.flags & 4194304 /* TypeFlags.Index */) {
68867 // We're inferring from some source type S to a homomorphic mapped type { [P in keyof T]: X },
68868 // where T is a type variable. Use inferTypeForHomomorphicMappedType to infer a suitable source
68869 // type and then make a secondary inference from that type to T. We make a secondary inference
68870 // such that direct inferences to T get priority over inferences to Partial<T>, for example.
68871 var inference = getInferenceInfoForType(constraintType.type);
68872 if (inference && !inference.isFixed && !isFromInferenceBlockedSource(source)) {
68873 var inferredType = inferTypeForHomomorphicMappedType(source, target, constraintType);
68874 if (inferredType) {
68875 // We assign a lower priority to inferences made from types containing non-inferrable
68876 // types because we may only have a partial result (i.e. we may have failed to make
68877 // reverse inferences for some properties).
68878 inferWithPriority(inferredType, inference.typeParameter, ts.getObjectFlags(source) & 262144 /* ObjectFlags.NonInferrableType */ ?
68879 16 /* InferencePriority.PartialHomomorphicMappedType */ :
68880 8 /* InferencePriority.HomomorphicMappedType */);
68881 }
68882 }
68883 return true;
68884 }
68885 if (constraintType.flags & 262144 /* TypeFlags.TypeParameter */) {
68886 // We're inferring from some source type S to a mapped type { [P in K]: X }, where K is a type
68887 // parameter. First infer from 'keyof S' to K.
68888 inferWithPriority(getIndexType(source), constraintType, 32 /* InferencePriority.MappedTypeConstraint */);
68889 // If K is constrained to a type C, also infer to C. Thus, for a mapped type { [P in K]: X },
68890 // where K extends keyof T, we make the same inferences as for a homomorphic mapped type
68891 // { [P in keyof T]: X }. This enables us to make meaningful inferences when the target is a
68892 // Pick<T, K>.
68893 var extendedConstraint = getConstraintOfType(constraintType);
68894 if (extendedConstraint && inferToMappedType(source, target, extendedConstraint)) {
68895 return true;
68896 }
68897 // If no inferences can be made to K's constraint, infer from a union of the property types
68898 // in the source to the template type X.
68899 var propTypes = ts.map(getPropertiesOfType(source), getTypeOfSymbol);
68900 var indexTypes = ts.map(getIndexInfosOfType(source), function (info) { return info !== enumNumberIndexInfo ? info.type : neverType; });
68901 inferFromTypes(getUnionType(ts.concatenate(propTypes, indexTypes)), getTemplateTypeFromMappedType(target));
68902 return true;
68903 }
68904 return false;
68905 }
68906 function inferToConditionalType(source, target) {
68907 if (source.flags & 16777216 /* TypeFlags.Conditional */) {
68908 inferFromTypes(source.checkType, target.checkType);

Callers 1

inferFromObjectTypesFunction · 0.85

Calls 12

getInferenceInfoForTypeFunction · 0.85
inferWithPriorityFunction · 0.85
getIndexTypeFunction · 0.85
getConstraintOfTypeFunction · 0.85
getPropertiesOfTypeFunction · 0.85
getIndexInfosOfTypeFunction · 0.85
inferFromTypesFunction · 0.85
getUnionTypeFunction · 0.85
mapMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…