Skip to content

Commit aa023eb

Browse files
committed
Remove TypeFlags.UnionOfPrimitiveTypes
1 parent 85dbc04 commit aa023eb

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

src/compiler/checker.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9012,7 +9012,7 @@ namespace ts {
90129012
neverType;
90139013
}
90149014
}
9015-
return getUnionTypeFromSortedList(typeSet, includes & TypeFlags.NotPrimitiveUnion ? 0 : TypeFlags.UnionOfPrimitiveTypes, aliasSymbol, aliasTypeArguments);
9015+
return getUnionTypeFromSortedList(typeSet, !(includes & TypeFlags.NotPrimitiveUnion), aliasSymbol, aliasTypeArguments);
90169016
}
90179017

90189018
function getUnionTypePredicate(signatures: ReadonlyArray<Signature>): TypePredicate | undefined {
@@ -9052,7 +9052,7 @@ namespace ts {
90529052
}
90539053

90549054
// This function assumes the constituent type list is sorted and deduplicated.
9055-
function getUnionTypeFromSortedList(types: Type[], unionOfUnitTypes: TypeFlags, aliasSymbol?: Symbol, aliasTypeArguments?: ReadonlyArray<Type>): Type {
9055+
function getUnionTypeFromSortedList(types: Type[], primitiveTypesOnly: boolean, aliasSymbol?: Symbol, aliasTypeArguments?: ReadonlyArray<Type>): Type {
90569056
if (types.length === 0) {
90579057
return neverType;
90589058
}
@@ -9063,9 +9063,10 @@ namespace ts {
90639063
let type = unionTypes.get(id);
90649064
if (!type) {
90659065
const propagatedFlags = getPropagatingFlagsOfTypes(types, /*excludeKinds*/ TypeFlags.Nullable);
9066-
type = <UnionType>createType(TypeFlags.Union | propagatedFlags | unionOfUnitTypes);
9066+
type = <UnionType>createType(TypeFlags.Union | propagatedFlags);
90679067
unionTypes.set(id, type);
90689068
type.types = types;
9069+
type.primitiveTypesOnly = primitiveTypesOnly;
90699070
/*
90709071
Note: This is the alias symbol (or lack thereof) that we see when we first encounter this union type.
90719072
For aliases of identical unions, eg `type T = A | B; type U = A | B`, the symbol of the first alias encountered is the aliasSymbol.
@@ -9157,13 +9158,16 @@ namespace ts {
91579158
// other unions and return true. Otherwise, do nothing and return false.
91589159
function intersectUnionsOfPrimitiveTypes(types: Type[]) {
91599160
let unionTypes: UnionType[] | undefined;
9160-
const index = findIndex(types, t => (t.flags & TypeFlags.UnionOfPrimitiveTypes) !== 0);
9161+
const index = findIndex(types, t => !!(t.flags & TypeFlags.Union) && (<UnionType>t).primitiveTypesOnly);
9162+
if (index < 0) {
9163+
return false;
9164+
}
91619165
let i = index + 1;
91629166
// Remove all but the first union of primitive types and collect them in
91639167
// the unionTypes array.
91649168
while (i < types.length) {
91659169
const t = types[i];
9166-
if (t.flags & TypeFlags.UnionOfPrimitiveTypes) {
9170+
if (t.flags & TypeFlags.Union && (<UnionType>t).primitiveTypesOnly) {
91679171
(unionTypes || (unionTypes = [<UnionType>types[index]])).push(<UnionType>t);
91689172
orderedRemoveItemAt(types, i);
91699173
}
@@ -9190,7 +9194,7 @@ namespace ts {
91909194
}
91919195
}
91929196
// Finally replace the first union with the result
9193-
types[index] = getUnionTypeFromSortedList(result, TypeFlags.UnionOfPrimitiveTypes);
9197+
types[index] = getUnionTypeFromSortedList(result, /*primitiveTypesOnly*/ true);
91949198
return true;
91959199
}
91969200

@@ -9232,7 +9236,7 @@ namespace ts {
92329236
return typeSet[0];
92339237
}
92349238
if (includes & TypeFlags.Union) {
9235-
if (includes & TypeFlags.UnionOfPrimitiveTypes && intersectUnionsOfPrimitiveTypes(typeSet)) {
9239+
if (intersectUnionsOfPrimitiveTypes(typeSet)) {
92369240
// When the intersection creates a reduced set (which might mean that *all* union types have
92379241
// disappeared), we restart the operation to get a new set of combined flags. Once we have
92389242
// reduced we'll never reduce again, so this occurs at most once.
@@ -11809,12 +11813,14 @@ namespace ts {
1180911813
}
1181011814

1181111815
// Keep this up-to-date with the same logic within `getApparentTypeOfContextualType`, since they should behave similarly
11812-
function findMatchingDiscriminantType(source: Type, target: UnionOrIntersectionType) {
11813-
const sourceProperties = getPropertiesOfObjectType(source);
11814-
if (sourceProperties) {
11815-
const sourcePropertiesFiltered = findDiscriminantProperties(sourceProperties, target);
11816-
if (sourcePropertiesFiltered) {
11817-
return discriminateTypeByDiscriminableItems(target, map(sourcePropertiesFiltered, p => ([() => getTypeOfSymbol(p), p.escapedName] as [() => Type, __String])), isRelatedTo);
11816+
function findMatchingDiscriminantType(source: Type, target: Type) {
11817+
if (target.flags & TypeFlags.Union) {
11818+
const sourceProperties = getPropertiesOfObjectType(source);
11819+
if (sourceProperties) {
11820+
const sourcePropertiesFiltered = findDiscriminantProperties(sourceProperties, target);
11821+
if (sourcePropertiesFiltered) {
11822+
return discriminateTypeByDiscriminableItems(<UnionType>target, map(sourcePropertiesFiltered, p => ([() => getTypeOfSymbol(p), p.escapedName] as [() => Type, __String])), isRelatedTo);
11823+
}
1181811824
}
1181911825
}
1182011826
return undefined;
@@ -14846,7 +14852,7 @@ namespace ts {
1484614852
if (type.flags & TypeFlags.Union) {
1484714853
const types = (<UnionType>type).types;
1484814854
const filtered = filter(types, f);
14849-
return filtered === types ? type : getUnionTypeFromSortedList(filtered, type.flags & TypeFlags.UnionOfPrimitiveTypes);
14855+
return filtered === types ? type : getUnionTypeFromSortedList(filtered, (<UnionType>type).primitiveTypesOnly);
1485014856
}
1485114857
return f(type) ? type : neverType;
1485214858
}

src/compiler/types.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3838,13 +3838,11 @@ namespace ts {
38383838
/* @internal */
38393839
FreshLiteral = 1 << 27, // Fresh literal or unique type
38403840
/* @internal */
3841-
UnionOfPrimitiveTypes = 1 << 28, // Type is union of primitive types
3841+
ContainsWideningType = 1 << 28, // Type is or contains undefined or null widening type
38423842
/* @internal */
3843-
ContainsWideningType = 1 << 29, // Type is or contains undefined or null widening type
3843+
ContainsObjectLiteral = 1 << 29, // Type is or contains object literal type
38443844
/* @internal */
3845-
ContainsObjectLiteral = 1 << 30, // Type is or contains object literal type
3846-
/* @internal */
3847-
ContainsAnyFunctionType = 1 << 31, // Type is or contains the anyFunctionType
3845+
ContainsAnyFunctionType = 1 << 30, // Type is or contains the anyFunctionType
38483846

38493847
/* @internal */
38503848
AnyOrUnknown = Any | Unknown,
@@ -4077,7 +4075,10 @@ namespace ts {
40774075
couldContainTypeVariables: boolean;
40784076
}
40794077

4080-
export interface UnionType extends UnionOrIntersectionType { }
4078+
export interface UnionType extends UnionOrIntersectionType {
4079+
/* @internal */
4080+
primitiveTypesOnly: boolean;
4081+
}
40814082

40824083
export interface IntersectionType extends UnionOrIntersectionType {
40834084
/* @internal */

0 commit comments

Comments
 (0)