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

Function removeSubtypes

test/fixtures/snapshot/typescript.js:61160–61222  ·  view source on GitHub ↗
(types, hasObjectTypes)

Source from the content-addressed store, hash-verified

61158 return includes;
61159 }
61160 function removeSubtypes(types, hasObjectTypes) {
61161 // [] and [T] immediately reduce to [] and [T] respectively
61162 if (types.length < 2) {
61163 return types;
61164 }
61165 var id = getTypeListId(types);
61166 var match = subtypeReductionCache.get(id);
61167 if (match) {
61168 return match;
61169 }
61170 // We assume that redundant primitive types have already been removed from the types array and that there
61171 // are no any and unknown types in the array. Thus, the only possible supertypes for primitive types are empty
61172 // object types, and if none of those are present we can exclude primitive types from the subtype check.
61173 var hasEmptyObject = hasObjectTypes && ts.some(types, function (t) { return !!(t.flags & 524288 /* TypeFlags.Object */) && !isGenericMappedType(t) && isEmptyResolvedType(resolveStructuredTypeMembers(t)); });
61174 var len = types.length;
61175 var i = len;
61176 var count = 0;
61177 while (i > 0) {
61178 i--;
61179 var source = types[i];
61180 if (hasEmptyObject || source.flags & 469499904 /* TypeFlags.StructuredOrInstantiable */) {
61181 // Find the first property with a unit type, if any. When constituents have a property by the same name
61182 // but of a different unit type, we can quickly disqualify them from subtype checks. This helps subtype
61183 // reduction of large discriminated union types.
61184 var keyProperty = source.flags & (524288 /* TypeFlags.Object */ | 2097152 /* TypeFlags.Intersection */ | 58982400 /* TypeFlags.InstantiableNonPrimitive */) ?
61185 ts.find(getPropertiesOfType(source), function (p) { return isUnitType(getTypeOfSymbol(p)); }) :
61186 undefined;
61187 var keyPropertyType = keyProperty && getRegularTypeOfLiteralType(getTypeOfSymbol(keyProperty));
61188 for (var _i = 0, types_10 = types; _i < types_10.length; _i++) {
61189 var target = types_10[_i];
61190 if (source !== target) {
61191 if (count === 100000) {
61192 // After 100000 subtype checks we estimate the remaining amount of work by assuming the
61193 // same ratio of checks per element. If the estimated number of remaining type checks is
61194 // greater than 1M we deem the union type too complex to represent. This for example
61195 // caps union types at 1000 unique object types.
61196 var estimatedCount = (count / (len - i)) * len;
61197 if (estimatedCount > 1000000) {
61198 ts.tracing === null || ts.tracing === void 0 ? void 0 : ts.tracing.instant("checkTypes" /* tracing.Phase.CheckTypes */, "removeSubtypes_DepthLimit", { typeIds: types.map(function (t) { return t.id; }) });
61199 error(currentNode, ts.Diagnostics.Expression_produces_a_union_type_that_is_too_complex_to_represent);
61200 return undefined;
61201 }
61202 }
61203 count++;
61204 if (keyProperty && target.flags & (524288 /* TypeFlags.Object */ | 2097152 /* TypeFlags.Intersection */ | 58982400 /* TypeFlags.InstantiableNonPrimitive */)) {
61205 var t = getTypeOfPropertyOfType(target, keyProperty.escapedName);
61206 if (t && isUnitType(t) && getRegularTypeOfLiteralType(t) !== keyPropertyType) {
61207 continue;
61208 }
61209 }
61210 if (isTypeRelatedTo(source, target, strictSubtypeRelation) && (!(ts.getObjectFlags(getTargetType(source)) & 1 /* ObjectFlags.Class */) ||
61211 !(ts.getObjectFlags(getTargetType(target)) & 1 /* ObjectFlags.Class */) ||
61212 isTypeDerivedFrom(source, target))) {
61213 ts.orderedRemoveItemAt(types, i);
61214 break;
61215 }
61216 }
61217 }

Callers 1

getUnionTypeFunction · 0.85

Calls 15

getTypeListIdFunction · 0.85
isGenericMappedTypeFunction · 0.85
isEmptyResolvedTypeFunction · 0.85
getPropertiesOfTypeFunction · 0.85
isUnitTypeFunction · 0.85
getTypeOfSymbolFunction · 0.85
getTypeOfPropertyOfTypeFunction · 0.85
isTypeRelatedToFunction · 0.85
getTargetTypeFunction · 0.85
isTypeDerivedFromFunction · 0.85

Tested by

no test coverage detected