Skip to content

Commit dc04b97

Browse files
authored
Merge pull request #28027 from Microsoft/simplifyAddTypeToUnion
Simplify addTypeToUnion function
2 parents 72244c5 + 033ea4c commit dc04b97

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

src/compiler/checker.ts

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8855,46 +8855,37 @@ namespace ts {
88558855
return false;
88568856
}
88578857

8858-
function addTypeToUnion(typeSet: Type[], includes: TypeFlags, type: Type, noReduction: boolean) {
8858+
function addTypeToUnion(typeSet: Type[], includes: TypeFlags, type: Type) {
88598859
const flags = type.flags;
88608860
if (flags & TypeFlags.Union) {
8861-
return addTypesToUnion(typeSet, includes, (<UnionType>type).types, noReduction);
8861+
return addTypesToUnion(typeSet, includes, (<UnionType>type).types);
88628862
}
88638863
// We ignore 'never' types in unions. Likewise, we ignore intersections of unit types as they are
88648864
// another form of 'never' (in that they have an empty value domain). We could in theory turn
88658865
// intersections of unit types into 'never' upon construction, but deferring the reduction makes it
88668866
// easier to reason about their origin.
88678867
if (!(flags & TypeFlags.Never || flags & TypeFlags.Intersection && isEmptyIntersectionType(<IntersectionType>type))) {
88688868
includes |= flags & ~TypeFlags.ConstructionFlags;
8869-
if (noReduction) {
8870-
addTypeToTypeSet(typeSet, type);
8871-
}
8872-
else if (flags & TypeFlags.AnyOrUnknown) {
8873-
if (type === wildcardType) includes |= TypeFlags.Wildcard;
8874-
}
8875-
else if (!strictNullChecks && flags & TypeFlags.Nullable) {
8869+
if (type === wildcardType) includes |= TypeFlags.Wildcard;
8870+
if (!strictNullChecks && flags & TypeFlags.Nullable) {
88768871
if (!(flags & TypeFlags.ContainsWideningType)) includes |= TypeFlags.NonWideningType;
88778872
}
88788873
else {
8879-
addTypeToTypeSet(typeSet, type);
8874+
const len = typeSet.length;
8875+
const index = len && type.id > typeSet[len - 1].id ? ~len : binarySearch(typeSet, type, getTypeId, compareValues);
8876+
if (index < 0) {
8877+
typeSet.splice(~index, 0, type);
8878+
}
88808879
}
88818880
}
88828881
return includes;
88838882
}
88848883

8885-
function addTypeToTypeSet(typeSet: Type[], type: Type) {
8886-
const len = typeSet.length;
8887-
const index = len && type.id > typeSet[len - 1].id ? ~len : binarySearch(typeSet, type, getTypeId, compareValues);
8888-
if (index < 0) {
8889-
typeSet.splice(~index, 0, type);
8890-
}
8891-
}
8892-
88938884
// Add the given types to the given type set. Order is preserved, duplicates are removed,
88948885
// and nested types of the given kind are flattened into the set.
8895-
function addTypesToUnion(typeSet: Type[], includes: TypeFlags, types: ReadonlyArray<Type>, noReduction: boolean): TypeFlags {
8886+
function addTypesToUnion(typeSet: Type[], includes: TypeFlags, types: ReadonlyArray<Type>): TypeFlags {
88968887
for (const type of types) {
8897-
includes = addTypeToUnion(typeSet, includes, type, noReduction);
8888+
includes = addTypeToUnion(typeSet, includes, type);
88988889
}
88998890
return includes;
89008891
}
@@ -8971,7 +8962,7 @@ namespace ts {
89718962
return types[0];
89728963
}
89738964
const typeSet: Type[] = [];
8974-
const includes = addTypesToUnion(typeSet, 0, types, unionReduction === UnionReduction.None);
8965+
const includes = addTypesToUnion(typeSet, 0, types);
89758966
if (unionReduction !== UnionReduction.None) {
89768967
if (includes & TypeFlags.AnyOrUnknown) {
89778968
return includes & TypeFlags.Any ? includes & TypeFlags.Wildcard ? wildcardType : anyType : unknownType;

0 commit comments

Comments
 (0)