@@ -9012,7 +9012,7 @@ namespace ts {
9012
9012
neverType;
9013
9013
}
9014
9014
}
9015
- return getUnionTypeFromSortedList(typeSet, includes & TypeFlags.NotPrimitiveUnion ? 0 : TypeFlags.UnionOfPrimitiveTypes , aliasSymbol, aliasTypeArguments);
9015
+ return getUnionTypeFromSortedList(typeSet, !( includes & TypeFlags.NotPrimitiveUnion) , aliasSymbol, aliasTypeArguments);
9016
9016
}
9017
9017
9018
9018
function getUnionTypePredicate(signatures: ReadonlyArray<Signature>): TypePredicate | undefined {
@@ -9052,7 +9052,7 @@ namespace ts {
9052
9052
}
9053
9053
9054
9054
// 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 {
9056
9056
if (types.length === 0) {
9057
9057
return neverType;
9058
9058
}
@@ -9063,9 +9063,10 @@ namespace ts {
9063
9063
let type = unionTypes.get(id);
9064
9064
if (!type) {
9065
9065
const propagatedFlags = getPropagatingFlagsOfTypes(types, /*excludeKinds*/ TypeFlags.Nullable);
9066
- type = <UnionType>createType(TypeFlags.Union | propagatedFlags | unionOfUnitTypes );
9066
+ type = <UnionType>createType(TypeFlags.Union | propagatedFlags);
9067
9067
unionTypes.set(id, type);
9068
9068
type.types = types;
9069
+ type.primitiveTypesOnly = primitiveTypesOnly;
9069
9070
/*
9070
9071
Note: This is the alias symbol (or lack thereof) that we see when we first encounter this union type.
9071
9072
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 {
9157
9158
// other unions and return true. Otherwise, do nothing and return false.
9158
9159
function intersectUnionsOfPrimitiveTypes(types: Type[]) {
9159
9160
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
+ }
9161
9165
let i = index + 1;
9162
9166
// Remove all but the first union of primitive types and collect them in
9163
9167
// the unionTypes array.
9164
9168
while (i < types.length) {
9165
9169
const t = types[i];
9166
- if (t.flags & TypeFlags.UnionOfPrimitiveTypes ) {
9170
+ if (t.flags & TypeFlags.Union && (<UnionType>t).primitiveTypesOnly ) {
9167
9171
(unionTypes || (unionTypes = [<UnionType>types[index]])).push(<UnionType>t);
9168
9172
orderedRemoveItemAt(types, i);
9169
9173
}
@@ -9190,7 +9194,7 @@ namespace ts {
9190
9194
}
9191
9195
}
9192
9196
// Finally replace the first union with the result
9193
- types[index] = getUnionTypeFromSortedList(result, TypeFlags.UnionOfPrimitiveTypes );
9197
+ types[index] = getUnionTypeFromSortedList(result, /*primitiveTypesOnly*/ true );
9194
9198
return true;
9195
9199
}
9196
9200
@@ -9232,7 +9236,7 @@ namespace ts {
9232
9236
return typeSet[0];
9233
9237
}
9234
9238
if (includes & TypeFlags.Union) {
9235
- if (includes & TypeFlags.UnionOfPrimitiveTypes && intersectUnionsOfPrimitiveTypes(typeSet)) {
9239
+ if (intersectUnionsOfPrimitiveTypes(typeSet)) {
9236
9240
// When the intersection creates a reduced set (which might mean that *all* union types have
9237
9241
// disappeared), we restart the operation to get a new set of combined flags. Once we have
9238
9242
// reduced we'll never reduce again, so this occurs at most once.
@@ -11809,12 +11813,14 @@ namespace ts {
11809
11813
}
11810
11814
11811
11815
// 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
+ }
11818
11824
}
11819
11825
}
11820
11826
return undefined;
@@ -14846,7 +14852,7 @@ namespace ts {
14846
14852
if (type.flags & TypeFlags.Union) {
14847
14853
const types = (<UnionType>type).types;
14848
14854
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 );
14850
14856
}
14851
14857
return f(type) ? type : neverType;
14852
14858
}
0 commit comments