Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
8f2e00f
Create separate types for equivalent aliased unions
ahejlsberg Dec 25, 2020
1e9ea77
Accept new baselines
ahejlsberg Dec 25, 2020
c9bea0c
Preserve original types for union types
ahejlsberg Dec 28, 2020
2e2048c
Accept new baselines
ahejlsberg Dec 28, 2020
44231c8
Preserve intersection origin for union types
ahejlsberg Dec 29, 2020
829285f
Accept new baselines
ahejlsberg Dec 29, 2020
536e41e
Accept new baselines
ahejlsberg Dec 29, 2020
cc5d0f2
Preserve aliases during relationship checks
ahejlsberg Dec 29, 2020
b2434fc
Accept new baselines
ahejlsberg Dec 29, 2020
6c1248e
Preserve aliases for intersection and indexed access types
ahejlsberg Dec 29, 2020
237e9ca
Accept new baselines
ahejlsberg Dec 29, 2020
d4dc215
Compute intersection-of-unions cross product without recursion
ahejlsberg Dec 30, 2020
11d2712
Accept new baselines
ahejlsberg Dec 30, 2020
39f82a8
Use denormalized type objects for origin / support 'keyof' origins
ahejlsberg Jan 1, 2021
d9a0f50
Accept new baselines
ahejlsberg Jan 1, 2021
e0d4774
Fix fourslash test
ahejlsberg Jan 1, 2021
21f61c0
Recursively extract named union types
ahejlsberg Jan 2, 2021
785d2b7
Accept new baselines
ahejlsberg Jan 2, 2021
d98caab
Map on union origin in mapType to better preserve aliases and origins
ahejlsberg Jan 3, 2021
260a665
Remove redundant call
ahejlsberg Jan 4, 2021
8597325
Accept new baselines
ahejlsberg Jan 4, 2021
5f7e126
Revert back to declared type when branches produce equivalent union
ahejlsberg Jan 4, 2021
8ef90e7
Accept new baselines
ahejlsberg Jan 4, 2021
4c9675c
Merge branch 'master' into preserveTypeAliases
ahejlsberg Jan 4, 2021
3fce1b9
Don't include denormal origin types in regular type statistics
ahejlsberg Jan 4, 2021
99355c5
Merge branch 'master' into preserveTypeAliases
ahejlsberg Jan 5, 2021
e388a26
Fix issue with unions not being marked primitive-only
ahejlsberg Jan 5, 2021
2c2d06d
Allow new alias to be associated with type alias instantiation
ahejlsberg Jan 8, 2021
4507270
Accept new baselines
ahejlsberg Jan 8, 2021
e794cb9
Merge branch 'master' into preserveTypeAliases
ahejlsberg Jan 8, 2021
4e123f5
Revert "Accept new baselines"
ahejlsberg Jan 9, 2021
8881f01
Revert "Allow new alias to be associated with type alias instantiation"
ahejlsberg Jan 9, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Preserve original types for union types
  • Loading branch information
ahejlsberg committed Dec 28, 2020
commit c9bea0cc353061565a819bb9f41d98a4825d68f2
38 changes: 18 additions & 20 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4554,7 +4554,7 @@ namespace ts {
: factory.createTypeReferenceNode(factory.createIdentifier("?"), /*typeArguments*/ undefined);
}
if (type.flags & (TypeFlags.Union | TypeFlags.Intersection)) {
const types = type.flags & TypeFlags.Union ? formatUnionTypes((<UnionType>type).types) : (<IntersectionType>type).types;
const types = type.flags & TypeFlags.Union ? formatUnionTypes((<UnionType>type).originalTypes || (<UnionType>type).types) : (<IntersectionType>type).types;
if (length(types) === 1) {
return typeToTypeNodeHelper(types[0], context);
}
Expand Down Expand Up @@ -13249,19 +13249,18 @@ namespace ts {
}
}

function findSingleAliasedUnion(types: readonly Type[]) {
let aliasedUnion: UnionType | undefined;
for (const t of types) {
if (t.flags & TypeFlags.Union && (<UnionType>t).aliasSymbol) {
if (!aliasedUnion) {
aliasedUnion = <UnionType>t;
}
else if (t !== aliasedUnion) {
return undefined;
}
function reduceOriginalTypes(types: readonly Type[], typeSet: readonly Type[]) {
const aliasedUnions = filter(types, t => !!(t.flags & TypeFlags.Union && t.aliasSymbol));
const result: Type[] = [];
for (const t of typeSet) {
if (!(t.flags & TypeFlags.Union && t.aliasSymbol) && !some(aliasedUnions, union => containsType((<UnionType>union).types, t))) {
result.push(t);
}
}
return aliasedUnion;
for (const t of aliasedUnions) {
insertType(result, t);
}
return result;
}

// We sort and deduplicate the constituent types based on object identity. If the subtypeReduction
Expand All @@ -13280,11 +13279,9 @@ namespace ts {
}
const typeSet: Type[] = [];
const includes = addTypesToUnion(typeSet, 0, types);
if (includes & TypeFlags.Union && !aliasSymbol) {
const aliasedUnion = findSingleAliasedUnion(types);
if (aliasedUnion && arraysEqual(typeSet, aliasedUnion.types)) {
return aliasedUnion;
}
const originalTypes = includes & TypeFlags.Union ? reduceOriginalTypes(types, typeSet) : undefined;
if (!aliasSymbol && originalTypes && originalTypes.length === 1) {
return originalTypes[0];
}
if (unionReduction !== UnionReduction.None) {
if (includes & TypeFlags.AnyOrUnknown) {
Expand Down Expand Up @@ -13313,7 +13310,7 @@ namespace ts {
}
const objectFlags = (includes & TypeFlags.NotPrimitiveUnion ? 0 : ObjectFlags.PrimitiveUnion) |
(includes & TypeFlags.Intersection ? ObjectFlags.ContainsIntersections : 0);
return getUnionTypeFromSortedList(typeSet, objectFlags, aliasSymbol, aliasTypeArguments);
return getUnionTypeFromSortedList(typeSet, objectFlags, originalTypes && originalTypes.length <= typeSet.length ? originalTypes : undefined, aliasSymbol, aliasTypeArguments);
}

function getUnionTypePredicate(signatures: readonly Signature[]): TypePredicate | undefined {
Expand Down Expand Up @@ -13349,20 +13346,21 @@ namespace ts {
}

// This function assumes the constituent type list is sorted and deduplicated.
function getUnionTypeFromSortedList(types: Type[], objectFlags: ObjectFlags, aliasSymbol?: Symbol, aliasTypeArguments?: readonly Type[]): Type {
function getUnionTypeFromSortedList(types: Type[], objectFlags: ObjectFlags, originalTypes?: Type[], aliasSymbol?: Symbol, aliasTypeArguments?: readonly Type[]): Type {
Comment thread
rbuckton marked this conversation as resolved.
if (types.length === 0) {
return neverType;
}
if (types.length === 1) {
return types[0];
}
const id = getTypeListId(types) + (aliasSymbol ? `@${getSymbolId(aliasSymbol)}` : "");
const id = getTypeListId(types) + (originalTypes ? `#${getTypeListId(originalTypes)}` : "") + (aliasSymbol ? `@${getSymbolId(aliasSymbol)}` : "");
let type = unionTypes.get(id);
if (!type) {
type = <UnionType>createType(TypeFlags.Union);
unionTypes.set(id, type);
type.objectFlags = objectFlags | getPropagatingFlagsOfTypes(types, /*excludeKinds*/ TypeFlags.Nullable);
type.types = types;
type.originalTypes = originalTypes;
/*
Note: This is the alias symbol (or lack thereof) that we see when we first encounter this union type.
For aliases of identical unions, eg `type T = A | B; type U = A | B`, the symbol of the first alias encountered is the aliasSymbol.
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5288,6 +5288,8 @@ namespace ts {
resolvedStringIndexType: IndexType;
/* @internal */
resolvedBaseConstraint: Type;
/* @internal */
originalTypes?: Type[];
}

export interface UnionType extends UnionOrIntersectionType {
Expand Down