MCPcopy Create free account
hub / github.com/nodejs/node / typeRelatedToDiscriminatedType

Function typeRelatedToDiscriminatedType

test/fixtures/snapshot/typescript.js:66204–66309  ·  view source on GitHub ↗
(source, target)

Source from the content-addressed store, hash-verified

66202 return 0 /* Ternary.False */;
66203 }
66204 function typeRelatedToDiscriminatedType(source, target) {
66205 // 1. Generate the combinations of discriminant properties & types 'source' can satisfy.
66206 // a. If the number of combinations is above a set limit, the comparison is too complex.
66207 // 2. Filter 'target' to the subset of types whose discriminants exist in the matrix.
66208 // a. If 'target' does not satisfy all discriminants in the matrix, 'source' is not related.
66209 // 3. For each type in the filtered 'target', determine if all non-discriminant properties of
66210 // 'target' are related to a property in 'source'.
66211 //
66212 // NOTE: See ~/tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithDiscriminatedUnion.ts
66213 // for examples.
66214 var sourceProperties = getPropertiesOfType(source);
66215 var sourcePropertiesFiltered = findDiscriminantProperties(sourceProperties, target);
66216 if (!sourcePropertiesFiltered)
66217 return 0 /* Ternary.False */;
66218 // Though we could compute the number of combinations as we generate
66219 // the matrix, this would incur additional memory overhead due to
66220 // array allocations. To reduce this overhead, we first compute
66221 // the number of combinations to ensure we will not surpass our
66222 // fixed limit before incurring the cost of any allocations:
66223 var numCombinations = 1;
66224 for (var _i = 0, sourcePropertiesFiltered_1 = sourcePropertiesFiltered; _i < sourcePropertiesFiltered_1.length; _i++) {
66225 var sourceProperty = sourcePropertiesFiltered_1[_i];
66226 numCombinations *= countTypes(getNonMissingTypeOfSymbol(sourceProperty));
66227 if (numCombinations > 25) {
66228 // We've reached the complexity limit.
66229 ts.tracing === null || ts.tracing === void 0 ? void 0 : ts.tracing.instant("checkTypes" /* tracing.Phase.CheckTypes */, "typeRelatedToDiscriminatedType_DepthLimit", { sourceId: source.id, targetId: target.id, numCombinations: numCombinations });
66230 return 0 /* Ternary.False */;
66231 }
66232 }
66233 // Compute the set of types for each discriminant property.
66234 var sourceDiscriminantTypes = new Array(sourcePropertiesFiltered.length);
66235 var excludedProperties = new ts.Set();
66236 for (var i = 0; i < sourcePropertiesFiltered.length; i++) {
66237 var sourceProperty = sourcePropertiesFiltered[i];
66238 var sourcePropertyType = getNonMissingTypeOfSymbol(sourceProperty);
66239 sourceDiscriminantTypes[i] = sourcePropertyType.flags & 1048576 /* TypeFlags.Union */
66240 ? sourcePropertyType.types
66241 : [sourcePropertyType];
66242 excludedProperties.add(sourceProperty.escapedName);
66243 }
66244 // Match each combination of the cartesian product of discriminant properties to one or more
66245 // constituents of 'target'. If any combination does not have a match then 'source' is not relatable.
66246 var discriminantCombinations = ts.cartesianProduct(sourceDiscriminantTypes);
66247 var matchingTypes = [];
66248 var _loop_20 = function (combination) {
66249 var hasMatch = false;
66250 outer: for (var _c = 0, _d = target.types; _c < _d.length; _c++) {
66251 var type = _d[_c];
66252 var _loop_21 = function (i) {
66253 var sourceProperty = sourcePropertiesFiltered[i];
66254 var targetProperty = getPropertyOfType(type, sourceProperty.escapedName);
66255 if (!targetProperty)
66256 return "continue-outer";
66257 if (sourceProperty === targetProperty)
66258 return "continue";
66259 // We compare the source property to the target in the context of a single discriminant type.
66260 var related = propertyRelatedTo(source, target, sourceProperty, targetProperty, function (_) { return combination[i]; }, /*reportErrors*/ false, 0 /* IntersectionState.None */, /*skipOptional*/ strictNullChecks || relation === comparableRelation);
66261 // If the target property could not be found, or if the properties were not related,

Callers 1

structuredTypeRelatedToFunction · 0.85

Calls 10

getPropertiesOfTypeFunction · 0.85
countTypesFunction · 0.85
_loop_20Function · 0.85
propertiesRelatedToFunction · 0.85
signaturesRelatedToFunction · 0.85
isTupleTypeFunction · 0.85
indexSignaturesRelatedToFunction · 0.85
addMethod · 0.65

Tested by

no test coverage detected